Advertisement
Adam_mz_

fib

Feb 9th, 2022
1,782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # int fib(int n) {
  2. #    if (n < 2)
  3. #        return n;
  4. #    else
  5. #        return fib(n-1) + fib(n-2);
  6. # }
  7.  
  8. .include "macrolib.s"
  9. .text
  10.  
  11. main:
  12. read_int(a0)
  13. jal fib
  14. print_int(a0)
  15.  
  16. li a7, 10
  17. ecall
  18.  
  19. fib:
  20. addi sp, sp, -12
  21. sw s0, 0(sp)
  22. sw s1, 4(sp)
  23. sw ra, 8(sp)
  24. li t0, 2
  25. bge a0, t0, else
  26. j fib_end
  27. else:
  28. mv s0, a0
  29. addi a0, a0, -1
  30. jal fib
  31. mv s1, a0
  32. mv a0, s0
  33. addi a0, a0, -2
  34. jal fib
  35. add a0, a0, s1
  36. fib_end:
  37. lw s0, 0(sp)
  38. lw s1, 4(sp)
  39. lw ra, 8(sp)
  40. addi sp, sp, 12
  41. jr ra
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement