我是汇编语言的新手,我不知道为什么我的条件语句没有按照我的意愿执行。我认为应该如何工作是当我在eax上使用cmp并在-1上执行时,如果输入为0或更大,则程序应跳转到标签为非负数;如果小于0,则应退出程序,但是无论输入如何,程序始终会跳转标签非负数,并执行分配给该标签的代码。我正在使用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
我希望程序打印“您输入的是非负数!”对于任何非负数,我希望该程序针对任何负数退出。而是程序总是打印“您输入了非负数!”。然后退出。
答案 0 :(得分:1)
您有以下代码:
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(满足条件)还是不采用JUMP(满足条件 ),都将执行以下指令:
mov edx, OFFSET testing ; Print message
call WriteString ; ...
call CrLf ; Print NextLine
因此,您始终可以在控制台上获得相同的结果:
您输入的是非负数!
为您提供正确的选择,请查看以下代码:
.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: