Advertisement
Guest User

Untitled

a guest
Nov 27th, 2018
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. bits 32
  2. global main
  3. extern scanf
  4. extern printf
  5.  
  6. section .data
  7.     n: dd 0
  8.     formatout: db "%d",10,0
  9. section .text
  10.  
  11. fibonacci:
  12. push    EBP         ; Retrieve parameter and put it
  13. push    EBX         ; Save previous parameter
  14. mov     EBP,ESP     ; into EBX register
  15. add     EBP,12      ;
  16. mov     EBX,[EBP]   ; EBX = Param
  17.  
  18. cmp     EBX,3       ; Check for base case
  19. jl    base        ; It is base if (n <= 1)
  20.  
  21. lea ecx,[ebx-3]
  22. push ecx            ; push N-1
  23. call    fibonacci   ; Calculate fibonacci for (EBX - 1)
  24. pop ecx             ; remove N-1 off the stack
  25.  
  26. push eax            ; save the result of fibonacci(N-1)
  27. lea ecx,[ebx-2]
  28. push ecx            ; push N-2
  29. call    fibonacci   ; Calculate fibonacci for (EBX - 2)
  30. pop ecx             ; remove N-2 off the stack
  31. pop ecx             ; ecx = fibonacci(N-1)
  32. add eax,ecx         ; eax = fibonacci(N-2) + fibonacci(N-1)
  33.  
  34. jmp end
  35. base:               ; Base case
  36. cmp ebx,2
  37. je dodaj7
  38. ;mov eax,1
  39. mov EAX,1
  40. jmp end           ; The result would be 1
  41. dodaj7:
  42. mov eax,7
  43.  
  44. end:
  45. pop     EBX         ; Restore previous parameter
  46. pop     EBP         ; Release EBP
  47. ret
  48.  
  49.  
  50.  
  51. main:
  52. push 15
  53. call fibonacci
  54. add esp, 4
  55. push eax
  56. push formatout
  57. call printf
  58. add esp,8
  59. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement