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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | . text .globl main main: # print user prompt string li $ v0 , 4 la $ a0 , input syscall # read integer - will be copied to v0 li $ v0 , 5 syscall # call procedure addi $ a0 , $ v0 , 0 # set a0 to the v0 value that the user just typed in jal add5 # print the answer: be sure to set a0 before v0 so we don't lose it! addi $ a0 , $ v0 , 0 # print the value that add5 just returned as v0 li $ v0 , 1 syscall # exit li $ v0 , 10 syscall add5: # Save one register on the stack addi $ sp , $ sp , -4 sw $ s0 , 0($ sp ) # add 5 to the input argument, and set it to the return value li $ s0 , 5 add $ v0 , $ a0 , $ s0 # Restore the register from the stack lw $ s0 , 0($ sp ) addi $ sp , $ sp , 4 # return jr $ ra . data input: . asciiz "Enter an integer\n" |
Questions
- Why was it important to set
a0
prior to executingsyscall 1
above? - 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
s0
to the stack in theadd5
function? - If we had used
t0
instead ofs0
inadd5
, would we have had to save it to the stack? What is the significance of this? - Why is the
syscall 10
call important in this code? What would happen if it was not present?
Model 2: Writing Procedures
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | . text .globl main main: la $ t0 , val1 la $ t1 , val2 lw $ t2 , 0($ t0 ) lw $ t3 , 0($ t1 ) sw $ t2 , 0($ t1 ) sw $ t3 , 0($ t0 ) # exit li $ v0 , 10 syscall . data val1: .word 5 val2: .word 6 |
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
.