title: How does this C code translate to MIPS instruction?
body: <p>I'm studying mips coding and have a solution to a given problem and I'm working to understand it and have defined each line to the best my knowledge, but I'm not picking up on a couple of lines on how they work mainly the last line with fib(n-1) + fib(n-2) how is that being returned? I see that after beq fails, and bne fails t1 is turned to 0 and exit is reached the value from t1 is stored to v0 for results/expressions and our (n) is reloaded before being deleted from the stack but I'm not quite seeing the fib(n-1) + fib(n-2) ? Help? Thank you!</p>
  
  <pre><code>C code:
  int fib(int n){
  if (n==0)
  return 0;
  else if (n == 1)
  return 1;
  else
  fib(n-1) + fib(n–2);
  </code></pre>
  
  <p>NEW** answer/ transliteration I worked on </p>
  
  #
  
  <pre><code>  #fib function
    loop:
    addi $sp, $sp, -4                 #creating item on stack -&gt; int n given caller value
    sw $ra, 0($sp)                       #saving address to stack
    addi $t0, $zero, $zero               #temp 0 is given value of 0
    beq 0($sp), $t0, exit                #if equal return 0  (if (n == 0)
    addi $t1, $zero, 1                   #temp1 gets value of 1
    beq 0($sp), $t1, exit                #if equal return 0 (else if(n==0)
    lw $t2, 0($sp)                       #storing n to temp 2 
    sub $t2, $t2, 1                      #n - 1
    lw $t3, 0($sp)                       #storing n to temp 3
    sub $t3, $t3, 2 #n-2
    add $t4, $t2, $t3                    # (fib(n-1) + fib( n-1) 
    sw 0($sp), $s4                       #storing n's new value back to its original       location
    bne 0($sp), $zero, loop              #jump to loop function with new value of n
    exit: jr $ra                         #return value of register address to caller
  </code></pre>
  
  <p>//OLD***Partially correct answer but not correct transliteration for fibanocci
  //---------------------------</p>
  
  <pre><code>compare:
  addi $sp, $sp, –4 #add immediate adjusts stack for one more item
  sw   $ra, 0($sp)  #saves return address on stack of our new item
  
  add $s0, $a0, $0  #add, stores argument 0 + (0) to s0
  add $s1, $a1, $0  #add, stores argument 0 + (0) to s1
  
  jal sub           #jump and link to subtract
  
  addi $t1, $0, 1   #add immediate, temp 1 = add 0 + 1
  beq $v0, $0, exit #branch on equal, if value in 0 is equal to zero go to -&gt; exit 
  slt $t2, $0, $v0  #set less than, if 0 &lt; value at 0  then temp2 equals 1 else 0
  bne $t2, $0, exit #branch on not equal, if temp2 not equal to zero go to -&gt; exit
  addi $t1, $0, $0  #add immediate, temp1 = 0 + 0
  
  exit:
  add $v0, $t1, $0  #add value at 0 = t1 + 0
  lw $ra, 0($sp)    #loads register address from stack 0()
  addi $sp, $sp, 4  #add immediate, deletes stack pointer pops it off stack
  jr $ra            #jump register, return to caller from return address
  
  sub:
  sub $v0, $a0, $a1 #subtract, value at 0 = argument 1 - argument 2
  jr $ra            #jump register, return to caller from return address
  </code></pre>
  
  <p>//</p>