Pipelining/Problem 5a

Use the following code fragment:

 Loop: LD R1, 0(R2) 
       DADDI R1, R1, #1 
       SD 0(R2), R1 
       DADDI R2, R2, #4 
       DSUB R4, R3, R2 
       BNEZ R4, Loop

Assume that the initial value of R3 is R2 + 396.

Assuming the branch has one delay slot, rewrite the code to fill in that slot. How many cycles does it take to execute one iteration of the loop now?

Solution

8 cycles are needed to execute one iteration of the loop.

 Loop: LD R1, 0(R2) 
       DADDI R1, R1, #1 
       DADDI R2, R2, #4 
       DSUB R4, R3, R2 
       BNEZ R4, Loop 
       SD -4(R2), R1