title: MIPS Fibonacci Using Recursion
body: <p>So, I am working on fibonacci in MIPS, and the rules are I need to have a preamble for a recursive method of solving the problem. My code currently is producing wrong output and I cannot identify which part should be edited. </p>
  
  <pre><code>.text
  
  main:   li $v0, 5
      syscall
      move $a0, $v0
      move $v0, $zero
      jal fibo
  
      move $t2, $v0
      li $v0, 1
      move $a0, $t2
      syscall
  
      li $v0, 10
      syscall
  
  
  fibo:   ####preamble####    #push from stack
          subu $sp, $sp, 32
          sw $ra, 0($sp)      #store return address
          sw $a0, 4($sp)
          sw $fp, 8($sp)
          sw $v0, 12($sp)
          addu $fp, $sp, 32
          ####preamble####    
  
  zero:   bne $a0, 0, one
          move $v0, $a0
          jr $ra  
  
  one:    bne $a0, 1, fn1
          move $v0, $a0
          jr $ra  
  
  fn1:    subi $a0, $a0, 1    #call for fibo(n-1)
          jal fibo            #recursive
  
          lw $a0, 4($sp)
          addi $a0, $a0, 1
  
          subi $a0, $a0, 2    #call for fibo(n-2) 
          jal fibo        
  
  result: lw $ra, 0($sp)      #load ra
          lw $fp, 8($sp)
          lw $t0, 12($sp)
          add $v0, $t0, $v0
          addu $sp, $sp, 32
          addu $fp, $fp, 32   
          jr $ra
  </code></pre>