Professional Documents
Culture Documents
fib(n)= fib(n-1)+fib(n-2),
value
explanation
01
00
00
00
80: ESP after CALL fib
EE
the address of return
EE
statement pushed into
EE
Stack
EE
84: ESP after PUSH EAX
01
inside the function fib
00
for n = 2
00
00
88: ESP after PUSH ECX
**
in function fib
**
**
**
92: ESP after the address of FF
the return statement
FF
FF
FF
96: ESP after inserting
02
parameter in C++ main
00
00
00
100: ESP initial value
MOV EAX, [ESP+2]
CMP EAX, 2 JAE ELS
ELS: DEC EAX
MOV ECX, EAX
PUSH EAX CALL fib
; EAX gets 2
; EAX =2 there is a jump to els
; EAX gets 1
; ECX gets 1
; the function will return to the beginning of fib
;with parameter 1
; ECX gets 1
; return to the caller function
; the execution came back to fib with n = 2 to the line ADD ESP, 4
; ESP = ESP + 4
; EAX = EAX +ECX = 1
;(EAX is 1 the value of fib(2) = fib(0) + fib(1)
;ECX is 1 ECX kept the value of fib(1))
POP ECX
RET
After the execution will return to main in C++ the command ADD ESP, 4 will be
executed in the background
Example:
n!= 1 ,
n! = n*(n-1)! ,
fact:
els:
dun:
n =0
n >0
global fact
push ecx
move eax, [esp+8]
cmp eax, 0
ja els
move eax, 1
jmp dun
mov ecx, eax
dec eax
push eax
call fact
add esp, 4
mul ecx
pop ecx
ret