mercredi 10 mars 2021

Assembly Language: JE/JNE as an if-else

So I'm trying to use JE/JNE to do an if-else statement. The problem is, it does not jump to where I want it to go.

Here is my code:

.model small
.stack
.data
    str1 db "Enter first number: ","$" 
    
    str2 db 13,10,"Enter second number: ","$"
    
    str3 db 13,10,"The product of "
    num1 db ?, " & "
    num2 db ?, " is "
    prod db ? ,"$" 
    
.code
    mov ax,@data
    mov ds,ax

    mov ah,9
    lea dx, str1
    int 21h
    
    mov ah,1
    int 21h ; takes user input for num1
    mov num1,al ; stores user input to variable input

    mov ah,9
    lea dx, str2
    int 21h
    
    mov ah,1
    int 21h ; takes user input for num2
    mov num2,al ; stores user input to variable input
    
    cmp al,1 ; compare num1 to 1
    jne equals2 ; jump to equals2 if num1 != 1, otherwise, it will just continue
        mov al, num2
        mov prod, al
        
        mov ah,9
        lea dx, str3
        int 21h
        jmp endCode

equals2:        
        mov al, num2
        ADD al, al
        mov prod, al
        sub prod, 30h
        
        mov ah,9
        lea dx, str3
        int 21h

endCode:
    mov ah,4ch
    int 21h

    mov ah,4ch
    int 21h
END

So if num1 == 1, it's fine.

But when num1 == 2 or more, it does not jump to equals2, instead, it just continues.

Here is the sample output:

Enter first number: 2
Enter second number: 4
The product of 2 & 4 is 6

Expected output:

Enter first number: 2
Enter second number: 4
The product of 2 & 4 is 8

Aucun commentaire:

Enregistrer un commentaire