← 🚰 Instruction Pipelining & Hazards
Subtopic 7 of 8

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

  1. Assume the register file is written in the first half and read in the second half of a cycle.
  2. No forwarding: stalls = 3 − distance (never negative).
  3. Distance 1 → 2 stalls, distance 2 → 1, distance 3 or more → 0.
  4. 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.
12345678
I1 ADD R1,..IFIDEXMEMWB
I2 SUB .,R1IFstallstallIDEXMEMWB

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?

Remember: No forwarding: stalls = 3 − distance.
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.