#$a0 = n #$v0 = retval main: nop; addi $a0, $0, 3; jal fib; #quit li $v0, 10 syscall fib: nop; beq $a0, $0, returnZero; addi $t0, $0, 1; beq $a0, $t0, returnOne; #push $ra # save return address sub $sp, $sp, 4; sw $ra, 0($sp); #push $a0 # save n sub $sp, $sp, 4; sw $a0, 0($sp); addi $a0, $a0, -1; jal fib; add $t0, $0, $v1; # t0 = retval addi $a0, $a0, -1; #push $t0 # save first retval sub $sp, $sp, 4; sw $t0, 0($sp); jal fib; #pop $t0 # get the first retval back lw $t0, 0($sp); addi $sp, $sp, 4; add $v1, $t0, $v1 # retval = retval1 + retval2 #pop $a0 # restore n lw $a0, 0($sp); add $sp, $sp, 4; #pop $ra # restore return address lw $ra, 0($sp); addi $sp, $sp, 4; jr $ra; # return returnZero: add $v1, $0, $0; jr $ra; # return returnOne: add $v1, $t0, $0; # retval = 1 + 0 = 1 jr $ra; # return