0

Hi I have a question on how this recursive factorial code below works (it's found in my assembly book). I understand most of the program except when the ReturnFact loop is called. I don't see it being called anywhere in the code or a jump statement directing it to there. How is this particular piece of code being executed? Any help would be greatly appreciated. Thank you.

Include Irvine32.inc

.code
main PROC
push 5 ; calc 5!
call Factorial ; calculate factorial (EAX)
call WriteDec ; display it
call Crlf
exit
main ENDP

Factorial PROC

push ebp
mov ebp,esp
mov eax,[ebp+8] ; get n
cmp eax,0 ; n  0?
ja L1 ; yes: continue
mov eax,1 ; no: return 1 as the value of 0!
jmp L2 ; and return to the caller

L1: dec eax
push eax ; Factorial(n1)
call Factorial

ReturnFact:
mov ebx,[ebp+8] ; get n
mul ebx ; EDX:EAX = EAX * EBX

L2: pop ebp ; return EAX
ret 4 ; clean up stack
Factorial ENDP
END main
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by gusano79
0

When the recursion started by the line call Factorial returns, execution continues at the line labeled with ReturnFact .

Note that this only happens when recursive calls are made; if the line jmp L2 is executed (i.e., it's time to terminate the recursion), the code at ReturnFact is skipped.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.