Question: Exercise 2.21 Assume that the stack and the static data segments are empty and that the stack and global pointers start at address 0x7fff fffc
Exercise 2.21 Assume that the stack and the static data segments are empty and that the stack and global pointers start at address 0x7fff fffc and 0x1000 8000, respectively. Assume the calling conventions as specifi ed in Figure 2.11 and that function inputs are passed using registers $a0 and returned in register $v0. Assume that leaf functions may only use saved registers.
a. main()
{
leaf_function(1);
}
int leaf_function (int f)
{
int result;
result = f + 1;
if (f > 5)
return result;
leaf_function(result);
}
b. int my_global = 100;
main()
{
int x = 10;
int y = 20;
int z;
z = my_function(x, my_global)
}
int my_function(int x, int y)
{
return x – y;
2.21.1 [5] <2.8> Show the contents of the stack and the static data segments after each function call.
2.21.2 [5] <2.8> Write MIPS code for the code in the table above.
2.21.3 [5] <2.8> If the leaf function could use temporary registers ($t0, $t1, etc.), write the MIPS code for the code in the table above.
The following three problems in this exercise refer to this function, written in MIPS assembler following the calling conventions from Figure 2.14:
a. f: sub $s0,$a0,$a3 sll $v0,$s0,0x1 add $v0,$a2,$v0 sub $v0,$v0,$a1 jr $ra
b. f: addi $sp,$sp,8 sw $ra,4($sp)
sw $s0,0($sp)
move $s0,$a2 jal g add $v0,$v0,$s0 lw $ra,4($sp)
lw $s0,0($sp)
addi $sp,$sp,–8 jr $ra 2.21.4 [10] <2.8> This code contains a mistake that violates the MIPS calling convention. What is this mistake and how should it be fi xed?
2.21.5 [10] <2.8> What is the C equivalent of this code? Assume that the function’s arguments are named
a, b,
c, etc. in the C version of the function.
2.21.6 [10] <2.8> At the point where this function is called register $a0, $a1, $a2, and $a3 have values 1, 100, 1000, and 30, respectively. What is the value returned by this function? If another function g is called from
f, assume that the value returned from g is always 500.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
