Reputation:
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!
Upvotes: 2
Views: 1118
Reputation: 30480
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 lw
instruction (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.
addi $sp, $sp, -4 # 1 read (CPU reads the instruction word)
sw $ra, 0($sp) # 1 read -------------- ## --------------
... Some unknown work is done ... # 100 reads (given in question)
addi $sp, $sp, 4 # 1 read (CPU reads the instruction word)
lw $ra, -4($sp) # 2 reads (CPU reads the instruction word, lw instruction also reads)
jr $ra # 1 read (CPU reads the instruction word)
Upvotes: 3