For the (pseudo) assembly code below, replace X, Y, P, and Q with the smallest set of instructions to save/restore values on the stack and update the stack pointer. Assume that procA and procB were written independently by two different programmers who are following the MIPS guidelines for caller-saved and callee-saved registers. In other words, the two programmers agree on the input arguments and return value of procB, but they can't see the code written by the other person.(50 points)
procA:
$s0 = ...
$s1 = ...
$s2 = ...
$t0 = ...
$t1 = ...
$t2 = ...
X
$a0 = ...
$a1 = ...
jal procB
Y
... = $s1
... = $t0
... = $t1
... = $a0
jr $ra

procB:
P
... = $a0
... = $a1
$s2 = ...
$t0 = ...
Q
jr $ra

Respuesta :

Answer:

Explanation:

Let us first consider the procedure procA; the caller in the example given.

  Some results: $s0,$s1,$s2, $t0,$t1 and $t2 are being stored by procA. Out of these registers, few registers are accessing by procA after a call to procB. But, procB might over-write these registers.

       Thus, procA need to save some registers into stack first before calling procB, .

      only $s1,$t0 and $t1 are being used after return from procB in the given example,

       Caller saves and restores only values in $t0-$t9, according to MIPS guidelines for caller-saved and callee-saved registers, .

       Thus, procA needs to save only $t0 and $t1.

    jal instruction overwrites the register $ra by writing the address, to which the control should jump back, after completing the instructions of procB, when procB is called,.

       Therefore, procA also need to save $ra into stack.

 ProcA is writing new values into $a0,$a2, procA must save $a0 and $a1 first before calling procB, .

     In the given example, after return from procB, only $a0 is being used. It is therefore enough to save $a0.

   Also, procA needs to save frame pointer, which points the start of the stack space for each procedure.

       Generally, as soon as the procedure begins, frame pointer is set to the current value of the stack pointer,.

Let us consider the procedure procB; the callee in the given example.

 The callee is responsible for saving values in $s0-$s7 and restoring them before returning to caller, this is according to MIPS guidelines for caller-saved and callee-saved registers,

   procB is expected to over-write the registers $s2 and $t0. Nonetheless, in the first two lines, procB might over-write the registers $s0 and $s1.

   Thus, procB is responsible for saving and restoring $s0,$s1 and $s2.

X:

We need to create space for 5 values on the stack since procA needs to save $a0,$ra,$t0,$t1 and $fp(frame pointer), . Each value(word) takes 4 bytes.

$sp = $sp – 20 # on the stack, create space for 5 values

sw $a0, 16($sp) # store the result in $a0 into the memory address

               # indicated by $sp+20

sw $ra, 12($sp) # save the second value on stack

sw $t0, 8($sp) # save the third value on stack

sw $t1, 4($sp) # save the fourth value on stack

sw $fp, 0($sp) #  To the stack pointer, save the frame pointer

$fp = $sp      #  To the stack pointer, set the frame pointer

Y:

lw $fp, 0($sp) #  from stack, start restoring values

lw $t1, 4($sp)

lw $t0, 8($sp)

lw $ra, 12($sp)

lw $a0, 16($sp)

$sp = $sp + 20 # decrease the size of the stack

P:

$sp = $sp – 12 #  for three values, create space on the stack

sw $s0, 0($sp) # save the value in $s0

sw $s1, 0($sp) # save the value in $s1

sw $s2, 0($sp) # save the value in $s2

Q:

lw $s0, 0($sp) #  from the stack, restore the value of $s0

lw $s1, 0($sp) #  from the stack, restore the value of $s1

lw $s2, 0($sp) #  from the stack, restore the value of $s2

$sp = $sp + 12 # decrease the stack size