Question: Consider the following C code that returns the N-th number in the Fibonacci sequence. int fib(int n) if (n0) else if (n--1) else return 0;
Consider the following C code that returns the N-th number in the Fibonacci sequence. int fib(int n) if (n0) else if (n--1) else return 0; return 1; return fib (n-1) + An implementation in MIPS assembly is shown below. fib: addi $sp, $sp, -12 sw ra, 8 (Ssp) sw $s0, 4(Ssp) sw $a, 0 (Ssp) bne $a0, $zero, test2 add $v0, $zero, $zero # make room on stack # push $ra # push $s0 # push $a0 # if n'-0, branch to test2 # ifn-0, fib (0)-0 exit test2: addi $t0, $zero, 1 bne $a0, $t0, recur add $v0, $zero, $to j exit # # if if n'=1, n==1, branch to fib (1)-1 recur recur addi a0, $a0, -1 jal fib add $s0, $v0, $zero addi $a0, $a0, -1 jal fib add $v0, $v0, $s0 # call fib (n-1) # save result to $s0 # call # fib (n-2) fib (n) # fib (n-2) . fib (n-1) exit: lw $a0, 0 (Ssp) # pop $a0 # pop $s0 # pop $ra # restore $sp lw ra, 8 (Ssp) addi $sp, $sp, 12 ra
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
