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 9
and is worth the entire 100 points.
Here are the things Im looking for in your solution:
There are no global variables other than I/O strings. There are a few number constants
(-1,1,2, and 20) which can be handled with instructions that use immediate values.
You should be able to do a while loop and the if/else constructs in assembly. I presented
them a couple of sections ago and the text details them in Section 9.
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 0?
result 4?
any registers 8-20
original $fp ?-16
$ra ?-12
number ?-8
return ?-4
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 fib(int); // forward declaration. Again, no assembly equivalent.
void main(void)
{
int number =-1;
while ((number <1)||(number >20))
{
printf("Enter a number (1-20): ");
scanf("%d", &number);
if (number <1)
{
printf("Number must be >0, try again.
");
}
if (number >20)
{
printf("Number must be <21, try again.
");
}
}
int value = fib(number);
printf("The fibonacci value is: %d
", value);
}
int fib(int n)
{
int result;
int temp;
if (n <2)
{
result = n;
}
else
{
temp = fib(n-2);
result = fib(n-1)+ temp;
}
return result;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!