大会中的GCD程序没有显示gcd的输出

时间:2015-05-11 05:13:03

标签: 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 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

2 个答案:

答案 0 :(得分:0)

我会直接告诉你这个代码有一个问题(可能有其他问题,而这只是一个立即弹出的问题):

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

这将切换寄存器,无论是什么,因为无论进位标志的状态如何,都可以在SWITCH运行代码。要么你明确地跳到那里,要么你到那里去。

我怀疑jmp L1(由于与jump if carry相同的原因,它在当前位置是多余的)应该紧跟在jc SWITCH之后,以便整个事情交换或不#39; t swap。

换句话说,比如:

        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.

答案 1 :(得分:0)

您的GCD函数缺少返回指令:

DONE:      <-- There should be a RET after the DONE label  
gcd ENDP