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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.