2.7Stall counting (classic 5-stage)
The idea
Counting stalls is simple once you see it: the farther apart the producer and consumer, the fewer stalls.
Think of it like this: The longer you wait before asking for the result, the more likely it is already ready.
Why it exists
GATE loves "how many cycles for this code" questions; this is the mechanical rule behind them.
How it works
- Assume the register file is written in the first half and read in the second half of a cycle.
- No forwarding: stalls = 3 − distance (never negative).
- Distance 1 → 2 stalls, distance 2 → 1, distance 3 or more → 0.
- If the register file cannot be read in the same cycle it is written, add one more stall to each.
Worked example: Dependent instruction 2 after the producer
Stalls = 3 − 2 = 1.
Now play with it
- →Click Distance 1, 2, 3 and count the stall cells.
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
|---|---|---|---|---|---|---|---|---|
| I1 ADD R1,.. | IF | ID | EX | MEM | WB | |||
| I2 SUB .,R1 | IF | stall | stall | ID | EX | MEM | WB |
Consumer is 1 instruction after the producer: 2 stall cycles (needs R1 written in WB, read in the second half of that cycle).
Check yourself
No forwarding, write in first half and read in second half. Consumer is 2 instructions after the producer. Stalls?
Exam notes (the dense version)
Assuming register write in 1st half / read in 2nd half of a cycle: no forwarding → dependent instr right after producer stalls 2 cycles, at distance 2 → 1 stall, distance ≥ 3 → 0. If that split-cycle trick is NOT assumed, use 3 / 2 / 1.
Previous year questions: Instruction Pipelining & Hazards (15)
PYQs are grouped by topic, so this is the full set for Instruction Pipelining & Hazards; try the ones that match this subtopic. Open all 15 on the topic page.