程序正在执行条件跳转,我不知道为什么
program is performing conditional jump and I don't know why
我是汇编语言的新手,我不知道为什么我的条件语句没有按照我想要的方式执行。我认为它应该如何工作是当我在 eax 和 -1 上使用 cmp 当输入为 0 或更大时程序应该跳转到非负标签,如果小于 0 则应该退出程序,但无论输入如何,程序总是跳转到标签 nonNegative 并执行分配给该标签的代码。我正在使用 Irvine32。
INCLUDE Irvine32.inc
.data
testing BYTE "you have entered a non-negative number! ", 0
.code
main PROC
call ReadInt
cmp eax, -1
JG nonNegative
nonNegative:
mov edx, OFFSET testing
call WriteString
call CrLf
exit
main ENDP
END main
我希望程序为任何非负数打印 "you have entered a non-negative number!",我希望程序为任何负数退出。相反,程序总是打印 "you have entered a non-negative number!" 然后退出。
你有这个代码:
call ReadInt ; Read the value to EAX
cmp eax, -1 ; determine JMP condition
JG nonNegative ; JMP if EAX > -1
nonNegative: ; Jump @target
mov edx, OFFSET testing ; Print message
call WriteString ; ...
call CrLf ; Print NextLine
问题是您正确地将 EAX 中的返回值与 -1
和
进行了比较
cmp eax, -1 ; determine JMP condition
并且您使用
正确执行了条件 JMP
JG nonNegative ; JMP if EAX > -1
但是你的错误是,这次跳转的JMP目标是下一行:
nonNegative: ; Jump @target
因此,如果 JUMP 被执行(条件满足)或不执行(条件 未 满足),将执行以下指令:
mov edx, OFFSET testing ; Print message
call WriteString ; ...
call CrLf ; Print NextLine
因此您总是在控制台上得到相同的结果:
you have entered a non-negative number!
要为您提供正确的选择,请查看此代码:
.data
negNumber BYTE "you have entered a negative number! ", 0
zeroOrAbove BYTE "you have entered a non-negative number! ", 0
.code
call ReadInt ; Read the value to EAX
cmp eax, -1 ; determine JMP condition
JG nonNegative ; JMP if EAX > -1
; this execution path was missing in your solution
mov edx, OFFSET negNumber ; Print message
call WriteString ; ...
call CrLf ; Print NextLine
jmp fin
nonNegative: ; Jump target
mov edx, OFFSET zeroOrAbove ; Print message
call WriteString ; ...
call CrLf ; Print NextLine
fin:
我是汇编语言的新手,我不知道为什么我的条件语句没有按照我想要的方式执行。我认为它应该如何工作是当我在 eax 和 -1 上使用 cmp 当输入为 0 或更大时程序应该跳转到非负标签,如果小于 0 则应该退出程序,但无论输入如何,程序总是跳转到标签 nonNegative 并执行分配给该标签的代码。我正在使用 Irvine32。
INCLUDE Irvine32.inc
.data
testing BYTE "you have entered a non-negative number! ", 0
.code
main PROC
call ReadInt
cmp eax, -1
JG nonNegative
nonNegative:
mov edx, OFFSET testing
call WriteString
call CrLf
exit
main ENDP
END main
我希望程序为任何非负数打印 "you have entered a non-negative number!",我希望程序为任何负数退出。相反,程序总是打印 "you have entered a non-negative number!" 然后退出。
你有这个代码:
call ReadInt ; Read the value to EAX
cmp eax, -1 ; determine JMP condition
JG nonNegative ; JMP if EAX > -1
nonNegative: ; Jump @target
mov edx, OFFSET testing ; Print message
call WriteString ; ...
call CrLf ; Print NextLine
问题是您正确地将 EAX 中的返回值与 -1
和
cmp eax, -1 ; determine JMP condition
并且您使用
正确执行了条件 JMPJG nonNegative ; JMP if EAX > -1
但是你的错误是,这次跳转的JMP目标是下一行:
nonNegative: ; Jump @target
因此,如果 JUMP 被执行(条件满足)或不执行(条件 未 满足),将执行以下指令:
mov edx, OFFSET testing ; Print message
call WriteString ; ...
call CrLf ; Print NextLine
因此您总是在控制台上得到相同的结果:
you have entered a non-negative number!
要为您提供正确的选择,请查看此代码:
.data
negNumber BYTE "you have entered a negative number! ", 0
zeroOrAbove BYTE "you have entered a non-negative number! ", 0
.code
call ReadInt ; Read the value to EAX
cmp eax, -1 ; determine JMP condition
JG nonNegative ; JMP if EAX > -1
; this execution path was missing in your solution
mov edx, OFFSET negNumber ; Print message
call WriteString ; ...
call CrLf ; Print NextLine
jmp fin
nonNegative: ; Jump target
mov edx, OFFSET zeroOrAbove ; Print message
call WriteString ; ...
call CrLf ; Print NextLine
fin: