If statement 汇编语言:JE/JNE作为if-else

If statement 汇编语言:JE/JNE作为if-else,if-statement,assembly,x86,dosbox,If Statement,Assembly,X86,Dosbox,因此,我尝试使用JE/JNE来执行if-else语句。问题是,它没有跳到我想要它去的地方 我在这里尝试的是使用ADD指令和compare和jump命令将2个用户输入的数字相乘 我想做一个if-else样式,但我不能继续,因为它不能跳转到其他标签 这是我的密码: .model small .stack .data str1 db "Enter first number: ","$" str2 db 13,10,"Ente

因此,我尝试使用
JE
/
JNE
来执行if-else语句。问题是,它没有跳到我想要它去的地方

我在这里尝试的是使用
ADD
指令和comparejump命令将2个用户输入的数字相乘

我想做一个if-else样式,但我不能继续,因为它不能跳转到其他标签

这是我的密码:

.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 num1,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, 49 ; convert hex to decimal
        
        mov ah,9
        lea dx, str3
        int 21h

endCode:
    mov ah,4ch
    int 21h

    mov ah,4ch
    int 21h
END

所以如果num1==1,就可以了

但当num1==2或更高时,它不会跳到equals2,而是继续

以下是示例输出:

预期产出:

顺便说一句,用户输入必须在1到9之间。

按0键时。。。9时,DOS.GetCharacter函数01h将返回48。。。57(ASCII码)

通常,您只需减去48即可将这些ASCII码转换为预期值。但是,此特定程序可以不使用该步骤。
您可以直接与ASCII值进行比较。这是名言,将使所有的差异

    mov al, num2
    cmp num1, '1'   <<< See the quotes 
    je  printCode
    add al, al
    sub al, 48      <<< Removing the duplicated bias
printcode:
    mov prod, al
    lea dx, str3
    mov ah, 09h
    int 21h
endCode:
mov al,num2

cmp num1,“1”您使用的系统调用返回单个ASCII字符。对于数字“0”到“9”,此字符的值为48到58。你需要先减去48把它转换成一个数字。我修改了这个问题,顺便说一句,ASCII字符与跳转命令有关吗?当然。与1的比较永远不会为真,因为如果输入1,系统调用将返回49(表示“1”的ASCII码)。在执行任何比较之前,必须将字符转换为数字。
Enter first number: 2
Enter second number: 4
The product of 2 & 4 is 8
    mov al, num2
    cmp num1, '1'   <<< See the quotes 
    je  printCode
    add al, al
    sub al, 48      <<< Removing the duplicated bias
printcode:
    mov prod, al
    lea dx, str3
    mov ah, 09h
    int 21h
endCode: