The code is from Chapter 8 of Intel assembly language programming (version 4), but I always feel that there are errors. The red code will never be executed logically. The following is the source code:
[Note: Because 32-bit registers are used, the maximum factorial that can be accommodated is 12! (479001600 )]
Title calculating a factorial (fact. ASM)
Include irvine32.inc
. Code
Main proc
Push 12; calc 12!
Call factorial; Calculate factorial (eax)
Returnmain:
Call writedec; display it
Call CRLF
Exit
Main endp
;------------------------------------------------------------------------
Factorial proc
; Calculates a factorial
; Es: [EBP + 8] = N, the number to calculate
; Returns: eax = the factorial of N
;------------------------------------------------------------------------
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
JMP L2
L1: Dec eax
Push eax; factorial (n-1)
Call factorial
; Instructions from this point on execute when each
; Recursive call returns.
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
Why is there a problem? When will the red code be executed?