# Doubt in pipelining forwarding in MIPS

I am fairly new to computer architecture and having a tough time solving problems based on pipelining. I was trying to solve a problem from this pdf I found on Google

I have a doubt in part b of problem 2 where we are asked How many cycles will it take to execute on the fully bypassed MIPS processor? I have two questions: Why is there a need for a stall between lw and sw instruction and secondly, how is the final answer being calculated anyway? I don't understand the part 1 + 63 · (6 + 3) + 1 · (6 + 2) = 576 at all. Also,even if I assume the stall between lw and sw, then if next iteration of loop starts at cycles 9,18 .. and so on. This should happen 64 times and the initial one cycle from li instruction and the 3 remaining cycles for the last execution of bnez means that total cycles = 1 + 64*9 + 3 = 580, right? I got 9 cycles as shown in the timing diagram , at 9th cycle ID of bnez is going on. for the last iteration, we won't execute the branch , but we need 3 more cycles to complete the bnez instruction.

EDIT: After checking out solution to the next part of same problem, it seems that the number of cycles are actually approximated for some reason. I thought that should be done only for large number of instructions (million or above). But I am not sure if I am correct :(

The reason for using 63 instead of 64 in the calculation is simple. Instruction that was fetched after bnez has to be flushed everytime, except for the last iteration.

For the last iteration, this instruction does not have to be flushed and saves one cycle of delay (as it is mentioned in the linked pdf), so we have the delay calculation as (6+2) instead of (6+3) for the last iteration.

Calculations:

There are 6 instructions in the loop. The Cycles Per Instruction (CPI) of the pipelined processor is 1. There are 2 stalls and 1 cycle is required to flush the instruction after bnez.

Total cycle count = (Number of instructions x CPI) + Number of stalls + Number of cycles required to flush the instruction after bnez

= (6x1) + 2 + 1

For last iteration the flush is not required. So it becomes 6+2.