Advertisement
Abreu-Hoff

Fibonacci hard code 14

Nov 19th, 2021
1,947
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDB 1.04 KB | None | 0 0
  1. .equ localSize, -16
  2. .equ fib, -8
  3.  
  4. result:
  5. .string "The 14th Fibonacci number is: %d\n"
  6.  
  7. #input:
  8. #.string "Enter your n value: "
  9. #.equ inputSz, 19
  10.  
  11. .text
  12. .global main
  13. .type main, @function
  14.  
  15. main:
  16. pushq %rbp
  17. movq %rsp, %rbp
  18. addq $localSize, %rsp
  19.  
  20. movl $14, %edi
  21. call Fibonacci
  22. movl %eax, %esi
  23. leaq result(%rip), %rdi
  24. movl $0, %eax
  25. call printf@PLT # PLT program location table
  26. subq $localSize, %rsp
  27. movq %rsp, %rbp
  28. popq %rbp
  29. ret
  30.  
  31. Fibonacci: #main function
  32. pushq %rbp
  33. movq %rsp, %rbp
  34. addq $localSize, %rsp #stack space yo
  35.  
  36. cmpl $1, %edi #base case comparison
  37. je baseFib1
  38.  
  39. cmpl $0, %edi #base case comparison
  40. jle baseFib0
  41.  
  42. movl %edi, n(%rbp)
  43. subl $1, %edi #subtract one, recurse
  44. call Fibonacci
  45.  
  46. movl %eax, fib(%rbp)
  47. movl n(%rbp), %edi #restore edi value
  48. subl $2, %edi #subtract two, recurse
  49. call Fibonacci
  50.  
  51. addl fib(%rbp), %eax
  52. jmp allDone
  53.  
  54. baseFib1:
  55. movl $1, %eax #Fib of 1 is the base case
  56. jmp allDone
  57.  
  58. baseFib0:
  59. movl $0, %eax #Fib of 0 is the base case
  60. jmp allDone
  61.  
  62. allDone:
  63. movq %rbp, %rsp
  64. popq %rbp
  65. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement