Advertisement
Guest User

Fibonacci - Recursively - Assembler - DLX

a guest
Jan 21st, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. Main:
  2.  
  3. //Initialize Registers
  4. ADD R3, R0, R0 //Initialize R3 - Helper for input verification
  5. ADD R4, R0, R0 //Initialize R4 - Our Result
  6. ADD R5, R0, R0 //Initialize R5 - Stackpointer
  7.  
  8. //Filter invalid input
  9. SLTI R2, R1, #0 // Check if input is smaller than 0
  10. BNEZ R2, EndInvInput // if so, go to end(invalid input)
  11. SUBI R3, R1, #41 // check if input is greater than 40
  12. SLTI R2, R3, #0 // ""
  13. BEQZ R2, EndInvinput // if so, go to end(invalid input)
  14.  
  15. SLEI R2, R1, #2 // if we have valid input but it is 0 or 1
  16. BNEZ R2, EndInput // go to EndInput because we know the values without calculation
  17.  
  18.  
  19. SUBI R1, R1, #1 // correct input in such a way, that we are not 0 based (like an array)
  20. // n=9 should return fib(9) and not fib(10)
  21. JAL Fibonacci // if we avoided all special cases and are still fine we call our recusrive method
  22. J End
  23.  
  24. Fibonacci:
  25. SW 1000(R5), R31 // store jump address on stack
  26. ADDI R5, R5, #4 // increment stackpointer
  27. SUBI R1, R1, #1 // decrement n by one ( fib(n-1))
  28. SLEI R2, R1, #1 // check if n is less than 1 or equal to 1
  29. ADD R4, R4, R2 // if that is the case we would add 1 to our result otherwise we would add 0 and move on
  30. BNEZ R2, skip_recursion1 // if that is the case we skip the next recursive call (return)
  31.  
  32. //rekursiver Aufruf
  33. SW 1000(R5), R1 // store current r1 on stack
  34. ADDI R5, R5, #4 // increment stackpointer
  35. JAL fibonacci // recursively call fibonacci
  36. SUBI R5, R5, #4 // decrement stackpointer after method has returned
  37. LW R1, 1000(R5) // load r1 from stack
  38.  
  39. skip_recursion1:
  40. SUBI R1, R1, #1 // we substract one from r1 again so we have minus two combined (fib(n-2))
  41. SLEI R2, R1, #1 // heck if n is less than 1 or equal to 1
  42. ADD R4, R4, R2 // if that is the case we would add 1 to our result otherwise we would add 0 and move on
  43. BNEZ R2, skip_recursion2 // if that is the case we skip the next recursive call (return)
  44.  
  45. //rekursiver Aufruf #2
  46. SW 1000(R5), R1 // store current r1 on stack
  47. ADDI R5, R5, #4 // increment stackpointer
  48. JAL fibonacci // recursively call fibonacci
  49. SUBI R5, R5, #4 // decrement stackpointer after method has returned
  50. LW R1, 1000(R5) // load r1 from stack
  51.  
  52. skip_recursion2:
  53. SUBI R5, R5, #4 // decrement stackpointer
  54. LW R31, 1000(R5) // load r31 from stack
  55. JR R31 // return where we left of
  56.  
  57. // We jump here directly from the start if our input is <=2
  58. // so we can handle our special cases 1 and 0 where we directly know the output
  59. // and adjust our program in such a way that the input does not have to be 0 based
  60. // (if input = 2)
  61. EndInput:
  62. SEQI R2, R1, #2 // check if input is equal to two
  63. BEQZ R2, EndOneZero // if not jump to one or zero case
  64. SUBI R1, R1, #1 // if it is we subtract one because fib(2) = 1 = fib(1)
  65. EndOneZero:
  66. ADD R4, R0, R1 // set result (r4) to zero or one depending on input
  67.  
  68. End:
  69. SW 2000(R0), R4 // store result in memory
  70.  
  71. EndInvInput:
  72. HALT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement