Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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;

image text in transcribed
image text in transcribed
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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Principles Of Database Systems With Internet And Java Applications

Authors: Greg Riccardi

1st Edition

020161247X, 978-0201612479

Students also viewed these Databases questions