title: Fibonacci calculator in C, compiled to mips, causes infinite loop
body: <p>I am trying to translate my fibonacci c code to MIPS assembly code, however, when I run the MIPS code to my MIPS simulator it doesn't seem to end.</p>
  <p>C Code:</p>
  <pre class="lang-c prettyprint-override"><code>int fibo(int n)
  {
      if(n&lt;2) return 1;
      else f(n-1)+f(n-2);
  }
  
  int main()
  {
      fibo(5);
  }
  </code></pre>
  <p>Assembly Code:</p>
  <pre><code>main: addi $sp, $sp, -4
  sw $ra, 0($sp)
  
  addi $a0, $zero, 5
  jal fibo; 
  
  lw $ra, 0($sp)
  addi $sp, $sp, 4
  
  fibo: addi $sp, $sp, -12
  sw $s0, 0($sp)
  sw $s1, 4($sp)
  sw $ra, 8($sp)
  
  slti $t0, $a0, 2
  beq $t0, $zero, ELSE
  
  addi $v0, $zero, 1
  jr $ra
  
  ELSE: addi $s0, $a0, 0
  addi $a0, $a0, -1
  jal fibo;
  
  addi $s1, $v0, 0
  addi $a0, $s0, -2
  jal fibo
  
  add $s1, $s1, $v0
  j EXIT
  
  EXIT: lw $s0, 0($sp)
  lw $s1, 4($sp)
  lw $ra, 8($sp)
  addi $sp, $sp, 12
  
  jr $ra
  </code></pre>