Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bits 32
- global main
- extern scanf
- extern printf
- section .data
- n: dd 0
- formatout: db "%d",10,0
- section .text
- fibonacci:
- push EBP ; Retrieve parameter and put it
- push EBX ; Save previous parameter
- mov EBP,ESP ; into EBX register
- add EBP,12 ;
- mov EBX,[EBP] ; EBX = Param
- cmp EBX,3 ; Check for base case
- jl base ; It is base if (n <= 1)
- lea ecx,[ebx-3]
- push ecx ; push N-1
- call fibonacci ; Calculate fibonacci for (EBX - 1)
- pop ecx ; remove N-1 off the stack
- push eax ; save the result of fibonacci(N-1)
- lea ecx,[ebx-2]
- push ecx ; push N-2
- call fibonacci ; Calculate fibonacci for (EBX - 2)
- pop ecx ; remove N-2 off the stack
- pop ecx ; ecx = fibonacci(N-1)
- add eax,ecx ; eax = fibonacci(N-2) + fibonacci(N-1)
- jmp end
- base: ; Base case
- cmp ebx,2
- je dodaj7
- ;mov eax,1
- mov EAX,1
- jmp end ; The result would be 1
- dodaj7:
- mov eax,7
- end:
- pop EBX ; Restore previous parameter
- pop EBP ; Release EBP
- ret
- main:
- push 15
- call fibonacci
- add esp, 4
- push eax
- push formatout
- call printf
- add esp,8
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement