CS274: Computer Architecture - MIPS Procedures
Activity Goals
The goals of this activity are:- To read and write procedure calls
- To save and restore the stack when calling procedures
- To use registers and save registers according to standard MIPS calling conventions
The Activity
Directions
Consider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to discuss your findings with the class, and the rest of the group should help that member prepare their response. Answer each question individually from the activity, and compare with your group to prepare for our whole-class discussion. After class, think about the questions in the reflective prompt and respond to those individually in your notebook. Report out on areas of disagreement or items for which you and your group identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.Model 1: MIPS Procedures
Questions
- Why was it important to set
a0prior to executingsyscall 1above? - Given that register values are 32 bits in size, why did we subtract 4 from the stack pointer prior to saving register
s0, and add 4 back to it when restoring the register from memory? - Why was it necessary to save register
s0to the stack in theadd5function? - If we had used
t0instead ofs0inadd5, would we have had to save it to the stack? What is the significance of this? - Why is the
syscall 10call important in this code? What would happen if it was not present?
Model 2: Writing Procedures
Questions
- What does this program do?
- Modify this program to execute the algorithm in a procedure, using the argument registers to correspond to the two memory addresses to be swapped. Call the procedure from
main.
Model 3: Key Formulas and Concepts Recap
A one-page summary of MIPS procedure calls.
The calling convention -- who uses which register:
| Registers | Purpose | Who must preserve them |
|---|---|---|
| $a0 - $a3 | Arguments into a procedure | Caller (they may be overwritten) |
| $v0 - $v1 | Return values out of a procedure | Nobody -- they carry the answer back |
| $t0 - $t9 | Temporaries | Caller: save them yourself before jal if you still need them |
| $s0 - $s7 | Saved values | Callee: a procedure that uses one must save and restore it on the stack |
| $ra | Return address, set by jal |
Callee, but only if it calls another procedure |
| $sp | Stack pointer: address of the top stack word | Callee: whatever you subtract, add back before jr $ra |
The call/return recipe:
- Caller places arguments in
$a0-$a3, then executesjal proc(sets$ra= PC + 4 and jumps). - Callee prologue: make room and save what the convention requires -- e.g.
addi $sp, $sp, -8,sw $ra, 4($sp),sw $s0, 0($sp). (Each saved register is one word = 4 bytes; the stack grows downward.) - Callee does its work and puts the result in
$v0. - Callee epilogue: restore in reverse --
lw $s0, 0($sp),lw $ra, 4($sp),addi $sp, $sp, 8-- thenjr $ra. - Caller finds the answer in
$v0. Micro-example:add5above saved one register, so it moved$spby exactly 4 down and 4 back up.
Stack frame layout (higher addresses at the top; $sp points at the last word pushed):
higher addresses +------------------------+ | caller's frame | +------------------------+ <- $sp before the call | saved $ra | 4($sp) +------------------------+ | saved $s0 | 0($sp) +------------------------+ <- $sp during the callee (after addi $sp, $sp, -8) | (free space) | lower addresses ... the stack grows DOWN
Glossary:
| Term | One-line definition |
|---|---|
| Caller / Callee | The procedure making the call / the procedure being called. |
jal |
Jump-and-link: saves the return address (PC + 4) in $ra, then jumps. |
jr $ra |
Jump-register: returns by setting the PC to the address in $ra. |
| Stack frame (activation record) | The block of stack memory one procedure call uses for its saved registers and locals. |
| Prologue / Epilogue | The entry code that saves registers / the exit code that restores them, in reverse order. |
| Caller-saved vs. callee-saved | $t registers are the caller's problem; $s registers, $sp, and $ra are the callee's. |
| Calling convention | The shared agreement about registers and the stack that lets any procedure call any other. |
| Push / Pop | Push = addi $sp, $sp, -4 + sw; pop = lw + addi $sp, $sp, 4. |