Question: Exercise 2.19 For the following problems, the table holds C code functions. Assume that the fi rst function listed in the table is called fi

Exercise 2.19 For the following problems, the table holds C code functions. Assume that the fi rst function listed in the table is called fi rst. You will be asked to translate these C code routines into MIPS Asembly.

a. int compare(int

a, int

b) {
if (sub

(a,

b) >= 0)
return 1;
else return 0;
}
int sub (int

a, int

b) {
return a–b;
}

b. int fi b_iter(int

a, int

b, int n){
if(n == 0)
return b;
else return fi b_iter(a+b,

a, n–1);
}
2.19.1 [15] <2.8> Implement the C code in the table in MIPS assembly. What is the total number of MIPS instructions needed to execute the function?
2.19.2 [5] <2.8> Functions can often be implemented by compilers “in-line”.
An in-line function is when the body of the function is copied into the program space, allowing the overhead of the function call to be eliminated. Implement an “in-line” version of the C code in the table in MIPS assembly. What is the reduction in the total number of MIPS assembly instructions needed to complete the function?
Assume that the C variable n is initialized to 5.
2.19.3 [5] <2.8> For each function call, show the contents of the stack after the function call is made. Assume the stack pointer is originally at addresss 0x7ffffffc, and follow the register conventions as specifi ed in Figure 2.11.
The following three problems in this exercise refer to a function f that calls another function func. The code for C function func is already compiled in another module using the MIPS calling convention from Figure 2.14. The function declaration for func is “int func(int

a, int b);”. The code for function f is as follows:

a. int f(int

a, int

b, int c){
return func(func(a,b),c);
}

b. int f(int

a, int

b, int c){
return func(a,b)+func(b,c);
}
2.19.4 [10] <2.8> Translate function f into MIPS assembler, also using the MIPS calling convention from Figure 2.14. If you need to use registers $t0 through $t7, use the lower-numbered registers fi rst.
2.19.5 [5] <2.8> Can we use the tail-call optimization in this function? If no, explain why not. If yes, what is the difference in the number of executed instructions in f with and without the optimization?

2.19.6 [5] <2.8> Right before your function f from Problem 2.19.4 returns, what do we know about contents of registers $t5, $s3, $ra, and $sp? Keep in mind that we know what the entire function f looks like, but for function func we only know its declaration.

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 Computer Organization And Design Questions!