Samuel .

asked • 03/18/15

Computer Architect questions: C TO MIPS

a)
Assume the following C code. Translate both functions to MIPS using the caller callee conventions as presented in class. Do not translate the print statement:
int main(){

int g=0; /*global variable */

int u= fib(5);

printf("%d",g); /*print the number of times you called fib */
}
int fib(int n){

g+=1 /*increase global variable every time you have a funtion call */

if (n==0) return 0;

else

if (n==1) return 1;

else

return fib(n-1) +fib(n-2);}


b)What is printed by the previous program? For arbitrary n what is the formula that gives g (i.e. How many times if fib(n) being called for arbitrary n)? Is fib effective? Explain

c)Rewrite a for Loop definition for fib and translate it to MIPS code.

d) Can you achieve the same effectiveness with a recursive definition? Give a fib implementation that behaves like a for loop given that your compiler optimizes tail call elimination(hint: the new function should have 3 arguments) .Give the MIPS code produced in such a compiler.

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.