Question: Study proc 2 . c and trace the program. What will be the output if you run the program? Compile and run proc 2 .

Study proc2.c and trace the program. What will be the output if you run the program? Compile and
run proc2.c in a terminal (or any IDE) and verify your answer.
2. Load proc2.s in MARS. This is the MIPS version of proc2.c. Do not assemble and run this
program, as there are errors due to the misuse of registers. Study the MAIN function and discuss with
your partners about what it does (compare it with the C version).
3. When MAIN calls SUM, SUM knows where to return to. Why? After SUM called SUB, what happens to the
address returning to MAIN? Discuss with your partners about how you would resolve this problem. Do
not attempt to fix it yet, as we have more problems to come.
4. The input argument (n) in function SUM is used to call the next function, SUB, as well as being added to
the return value. According to the register convention, the first argument of all function calls must be
stored in $a0. From line 27 of proc2.s, the value in $a0 is no longer the same as the input argument
of SUM (it has been changed to store input argument of SUB). We can resolve this problem by saving the
original $a0 into a temporary register, but we may eventually run out of registers if our program is large.
Discuss with your partners about how you would resolve this problem. Do not attempt to fix it yet.
5. Have a look at line 24. What happens to the original value in $s0 from MAIN after this statement is
executed? Is this a problem? Why? Discuss with your partners about how you would fix this problem.
Do not attempt to fix it yet.
6. Now we know that SUM needs to backup 3 values before calling SUB. Insert prologue and epilogue into
the code so the program will run correctly. (Hint: study function SUB, as it does not contain any errors)

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!