是/否程序无法正常工作

时间:2014-06-08 10:25:03

标签: assembly x86

我自己编写了以下代码:

mov ah, 9h
mov dx, offset cool
int 21h

mov ah, 1h
int 21h

cmp al, 79 
je _yes
jne _no

_yes:
    mov ah, 9h
    mov dx, offset yes
    int 21h 

_no:
    mov ah, 9h
    mov dx, offset no
    int 21h


mov ah, 4ch
int 21h

cool db "Are you cool [y/n]: $"
yes db 0ah, 0dh, "Yay, me too!$" 
no db 0ah, 0dh, "LOL, you'r a loser!$"

除了按y键之外,一切都很完美。如果我按下y以外的任何键,它会显示“LOL,你是一个失败者”,这正是我想要的。但是当我按下y键时,它显示“Yay,me too!”,这是正确的,但它也显示“LOL你是一个失败者”。意味着,如果你按下y键,它会显示“Yay,me too”,当你按下y以外的任何键时,它只是显示“LOL,你”失败者!“
如果我的解释不够好,请评论我会尝试更好地解释它 在此先感谢:)
PS我正在使用x86程序集,你可能会看到

1 个答案:

答案 0 :(得分:3)

_no部分位于代码中的_yes部分之后。标签只是程序中某个位置的便捷名称 - 它不是某种障碍。所以CPU不知道你只想要其中一个被执行,并且会继续执行它找到的任何指令。

如果要跳过代码,请添加跳转。例如:

_yes:
    mov ah, 9h
    mov dx, offset yes
    int 21h 
    jmp done  ; jump to done so that the "no" message isn't printed

_no:
....

done:
mov ah, 4ch
int 21h 
相关问题