166k views
4 votes
The exercises below refer to the following sequence of instructions. Assume it is executed on a 5-stage pipelined datapath:

add $s3, $s1, $s0
lw $s2, 4($s3)
lw $s1, 0($s4)
or $s2, $s3, $s2
sw $s2, 0($s3)

(a)If there is no forwarding or hazard detection, insert NOPs to ensure correct execution.
(b)Now, change and/or rearrange the code to minimize the number of NOPs needed. You can assume register $t0 can be used to hold temporary values in your modified code.
(c)If the processor has forwarding, but we forgot to implement the hazard detection unit, what happens when the original code executes?

User Mike Roosa
by
8.5k points

1 Answer

5 votes

Final answer:

When there is no forwarding or hazard detection, NOPs need to be inserted to avoid data hazards. Rearranging the code can minimize the number of NOPs. With forwarding but no hazard detection, data hazards can still occur.

Step-by-step explanation:

Part (a)

If there is no forwarding or hazard detection, we need to insert NOPs to avoid data hazards. In this code, there are two instructions that could create a hazard: lw $s2, 4($s3) and sw $s2, 0($s3). These instructions need to wait for the value in $s3 to be updated by the previous add instruction. Therefore, we need to insert two NOPs after the add instruction.

Part (b)

To minimize the number of NOPs, we can rearrange the code as follows:

add $s3, $s1, $s0

lw $s1, 0($s4)

lw $s2, 4($s3)

or $s2, $s3, $s2

sw $s2, 0($s3)

In this code, there is no need for any NOPs as the instructions are already in the correct order to avoid hazards.

Part (c)

If the processor has forwarding but no hazard detection, data hazards can still occur. Forwarding allows the processor to bypass the need to wait for the value to be written back to the register file, but if the data is not available due to a hazard, the instruction will still have to wait. In this case, the original code would have a hazard between the add, lw $s2, and sw $s2 instructions, as explained in part (a).

User Kieranpotts
by
7.4k points