Question: Assignment This one is fairly tough. It s almost like a project. For this one, you are the compiler. You need to convert the C
Assignment
This one is fairly tough. Its almost like a project. For this one, you are the compiler. You need
to convert the C program given below into a MIPS program. Youll want to use MARS and test
the code you write to be sure it works correctly. This one problem demonstrates all of Section
and is worth the entire points.
Here are the things Im looking for in your solution:
There are no global variables other than IO strings. There are a few number constants
and which can be handled with instructions that use immediate values.
You should be able to do a while loop and the ifelse constructs in assembly. I presented
them a couple of sections ago and the text details them in Section
The subroutine call to fib must pass values on the stack. In fact, because it is recursive, a
stack frame must be used. See my suggested layout for the stack frame.
Local variables for fib are required result and temp Dont just use registers because
recursive calls to fib will alter those registers and your code wont work correctly.
Again, see my suggested layout for the stack frame.
If you use a register in main to hold number you should preserve it before calling fib
and restore it after fib returns.
If you use registers in fib, youll use them in recursive calls, so preserve them in the stack
frame. I suggest you use the T registers, so you dont need to worry about the S
registers.
Suggested stack frame:
Pointers Usage $sp offset $fp offset
stack pointer temp
result
any registers
original $fp
$ra
number
return
frame pointer
Depending on how many registers you use in fib, the stack frame size may vary, which is
why $sp offsets below the registers or $fp offsets above the registers cannot be
determined. Once you determine which registers you will use, you can finish the stack
frame diagram.
The C program is on the following page.
#include not necessary for the assembly; youll use system calls
int fibint; forward declaration. Again, no assembly equivalent.
void mainvoid
int number ;
while number number
printfEnter a number : ;
scanfd &number;
if number
printfNumber must be try again.
;
if number
printfNumber must be try again.
;
int value fibnumber;
printfThe fibonacci value is: d
value;
int fibint n
int result;
int temp;
if n
result n;
else
temp fibn;
result fibn temp;
return result;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
