1

I understand that in a MIPS Pipeline, for a load instruction, data is read from cache at the 4th stage of instruction, which is the memory access stage. In case of other instructions (apart from load/store), the stage is responsible for writing data in result register to write back register and then in the next stage, it is written back to register file. But in case of load instruction after reading from cache, when is data written to destination register? Is it during 4th stage itself or during 5th stage. In case if this is done during 5th stage, is it written to write back register first, or is it directly written to register file?

1
  • 1
    In the case of a load instruction e.g. lw the value is written to the register during the write-back stage. The result is always written-back to a register in the write-back stage. Feb 17, 2014 at 20:43

1 Answer 1

4

So assuming you are talking about a standard 5-stage MIPS pipeline, the load instruction writes the destination register at the same time as any other instruction would - at the end of the writeback stage. So if we look at the following example:

cycle             0   1   2   3   4    5   6   7   8
LD R1, [MEM]      F | D | X | M | WB | 
ADDi R1, R1, #5     | F | D | D | D  | D | X | M | WB

Assuming there is no data-forwarding (or bypassing) in the pipeline, the loaded data will only become available the cycle after writeback. In case you have forwarding around the register file, it will look like this:

cycle             0   1   2   3   4    5   6   7 
LD R1, [MEM]      F | D | X | M | WB | 
ADDi R1, R1, #5     | F | D | D | D  | X | M | WB

As you can see, in cycle 4, where data was already loaded and sitting in the pipeline register between Mem and Wb stages, it was bypassed back to the Decode stage and written into the register file and into the pipeline register between the Decode and eXecute stages simultaneously.

With super aggressive bypassing you could get the data even one cycle earlier.

But in all of these examples, the LD instruction is still writing the destination register R1 at the end of Writeback stage.

3
  • 2
    The original MIPS (R2000) used a somewhat clever trick to reduce the delay in availability of values in the register file; it wrote to the register file in the first half of the cycle during writeback and read from the register file in the second half of decode (Table 2.7 in Modern Processor Design: Fundamentals of Superscalar Processors. Not only did this remove a cycle of forwarding but allowed the register file not to have a dedicated write port.)
    – user2467198
    Jul 25, 2014 at 14:21
  • 2
    The MIPS R2000 was also a bit odd in having a load delay slot. The instruction immediately after the load would not stall to wait for the load value to become available (Microprocessor without Interlocked Pipeline Stages). On a cache hit, the older value would be read from the register file, but on a cache miss (or an exception immediately after the load) the value returned by the load instruction would be read. It was intended that if no independent instruction could be inserted then a nop would be used (like the branch delay slot).
    – user2467198
    Jul 25, 2014 at 14:35
  • It might be helpful to note that writing to the register file in two different stages would introduce a structural hazard since two instructions might attempt to write to the register file at the same time.
    – user2467198
    Jul 25, 2014 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.