Loops 汇编中的GCD程序不';t显示gcd的输出
这将成功构建,并且键入整数没有问题。但是,我看不到gcd的结果,调试器在没有任何东西的情况下运行。是因为无限循环吗?我在这里完全迷路了。有人能找出我在这里遗漏了什么吗?请帮帮我这个怎么了Loops 汇编中的GCD程序不';t显示gcd的输出,loops,assembly,masm,irvine32,greatest-common-divisor,Loops,Assembly,Masm,Irvine32,Greatest Common Divisor,这将成功构建,并且键入整数没有问题。但是,我看不到gcd的结果,调试器在没有任何东西的情况下运行。是因为无限循环吗?我在这里完全迷路了。有人能找出我在这里遗漏了什么吗?请帮帮我这个怎么了 INCLUDE Irvine32.inc .data strA BYTE "Enter an integer A: ",0 strB BYTE "Enter an integer B: ",0 temp DWORD ? finalStr BYTE "GCD of the two integers i
INCLUDE Irvine32.inc
.data
strA BYTE "Enter an integer A: ",0
strB BYTE "Enter an integer B: ",0
temp DWORD ?
finalStr BYTE "GCD of the two integers is: ",0
.code
main PROC
call Clrscr
mainLoop:
mov edx,OFFSET strA
call WriteString
call ReadInt
mov temp, eax
call Crlf
mov edx, OFFSET strB
call WriteString
call ReadInt
mov ebx, eax
mov eax, temp
call Crlf
call GCD
mov edx, OFFSET finalStr
call WriteString
call WriteInt
call WaitMsg
jmp mainLoop
main ENDP
abs PROC
cmp eax, 0 ; see if we have a negative number
jge done
neg eax
done:
ret
abs ENDP
gcd PROC
call abs ;takes absolute value of both registers
mov temp, eax
mov eax, ebx
call abs
mov ebx, eax
mov eax, temp
cmp eax, ebx ; making sure we divide the bigger number by the smaller
jz DONE ; if numbers are equal, GCD is eax either way
jc SWITCH ;swaps if ebx is larger then eax
mov edx, 0
SWITCH: ;swaps values so eax is larger then ebx
mov temp, eax
mov eax, ebx
mov ebx, temp
mov edx, 0
jmp L1
L1: ;divides until remainder is 0, then eax is GCD
div ebx
cmp edx, 0
jz DONE
mov eax, edx
jmp L1
DONE:
gcd ENDP
END main
我将直截了当地告诉您该代码有一个错误(可能还有其他问题,那就是立即弹出的问题): 无论发生什么情况,这都将切换寄存器,因为无论进位标志的状态如何,您都会在
开关处运行代码。要么你直接跳到那里,要么你从那里掉下去
我怀疑jmp L1
(在当前位置是多余的,原因与如果进位跳转相同)应该紧跟在jc开关之后,这样整个东西要么交换,要么不交换
换句话说,类似于:
jnc L1 ; skip swap unless ebx > eax.
SWITCH: push eax ; don't need temp at all, use stack.
push ebx
pop eax
pop ebx
L1:
mov edx, 0 ; carry on.
您的GCD
函数缺少返回指令:
DONE: <-- There should be a RET after the DONE label
gcd ENDP
DONE:所以我把jmpl1
放在jc开关的后面。现在它显示一个错误:整数溢出。我该怎么办?@user4852500,没问题,但请务必看一看Michael的答案,这可能是您当前问题的原因,因为您由于缺少返回而开始运行非代码,所以跑向童话世界。如果是这样的话,你应该接受他的回答。
DONE: <-- There should be a RET after the DONE label
gcd ENDP