Search code examples
assemblyx86masmirvine32

procedure execute 'ret' but not execute it


I write the GreatCommonDivisor and use have declare ret down the repeat....until, but find it still go into the repeat ..... until and cause division by zero error.

I think that ret pop the address that the caller's next line, but why it jump into the repeat...until?

ps:eax is dividend and ebx is divisor.

Thx in advance.

INCLUDE Irvine32.inc

.data

.code
main PROC
mov eax, 75
mov ebx, 18
call gcd
main ENDP


gcd PROC 

or eax,eax;set SF
.IF Sign?
    Neg eax
.Else 
.EndIf

or ebx,ebx;set SF
.IF Sign?
    Neg ebx
.Else 
.EndIf

.Repeat 
mov edx, 0
div ebx

mov eax, ebx
mov ebx, edx    
.Until ebx <= 0

call WriteInt
ret
gcd ENDP

END main

Solution

  • Not sure why you made main a PROC. There's also a possibility that, due to how you've ordered your code, it will proceed with executing gcd again after reaching the end of main (I don't have MASM handy on this machine to verify this).

    I'd structure the program like this:

    INCLUDE Irvine32.inc
    
    .data
    .code
    
    gcd PROC 
    ; gcd implementation omitted for brevity..
    ret
    gcd ENDP
    
    main:
    mov eax, 75
    mov ebx, 18
    call gcd
    
    END main