We’re studying for our midterm in Computer Organization and Design on Tuesday, and none of us understand the following question:
procedure:
addi $sp, $sp, -4
sw $ra, 0($sp)
... Some unknown work is done ...
addi $sp, $sp, 4
lw $ra, -4($sp)
jr $ra
(1-H) Again consider the above code, and assume it works. If, during execution, the ’unknown
work’ section causes 100 words to be read from memory, how many words will be read from
memory during the execution of the entire procedure? (Consider all memory accesses.
Here is the answer: 106. 100 memory reads for the unknown work, plus 5 memory reads to read the instructions shown, plus 1 memory read during the “lw” instruction.
If anyone could help us understand exactly where each of these 6 memory reads occur, it would be greatly appreciated!
Let’s start with the obvious ones: 100 reads are performed in the “unknown work” section, that leaves 6 reads. One read is for the
lwinstruction (lw $ra, -4($sp)), which reads a words from memory. The final 5 reads are implicitly done by the CPU in the Instruction fetch stage.