You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Pipelining is a technique used in processor design to increase instruction throughput by overlapping the execution stages of multiple instructions. For the OCR H446 exam, you must understand how pipelining works, its advantages, the types of hazards that can occur, and how branch prediction addresses control hazards.
In a non-pipelined processor, each instruction must complete all stages (Fetch, Decode, Execute) before the next instruction begins. This leaves parts of the CPU idle during each stage.
Without pipelining (sequential execution):
Time: 1 2 3 4 5 6 7 8 9
Instr 1: F D E
Instr 2: F D E
Instr 3: F D E
Three instructions take 9 clock cycles.
With pipelining:
Time: 1 2 3 4 5
Instr 1: F D E
Instr 2: F D E
Instr 3: F D E
Three instructions take only 5 clock cycles. After the pipeline is full, one instruction completes every clock cycle.
Pipelining divides the processor into separate stages, each handled by dedicated hardware. While one instruction is being executed, the next is being decoded, and a third is being fetched — all at the same time.
For a k-stage pipeline processing n instructions (assuming no hazards):
For example, a 3-stage pipeline processing 100 instructions:
Modern processors use more than 3 stages. A typical pipeline might have 5 stages:
| Stage | Abbreviation | Action |
|---|---|---|
| Fetch | IF | Retrieve instruction from memory |
| Decode | ID | Decode instruction and read registers |
| Execute | EX | ALU performs the operation |
| Memory Access | MEM | Read from or write to memory (if needed) |
| Write Back | WB | Write the result back to a register |
Time: 1 2 3 4 5 6 7 8 9
Instr 1: IF ID EX MEM WB
Instr 2: IF ID EX MEM WB
Instr 3: IF ID EX MEM WB
Instr 4: IF ID EX MEM WB
Instr 5: IF ID EX MEM WB
After the first 5 cycles to fill the pipeline, one instruction completes every cycle.
A hazard is a situation that prevents the next instruction from executing in its designated clock cycle, causing the pipeline to stall (insert a "bubble" — an empty cycle).
A data hazard occurs when an instruction depends on the result of a previous instruction that has not yet completed.
Example:
ADD R1, R2, R3 ; R1 = R2 + R3
SUB R4, R1, R5 ; R4 = R1 - R5 <-- needs R1, but ADD has not written it yet
The SUB instruction needs the value of R1, but R1 has not been updated by ADD yet because ADD is still in its MEM or WB stage.
Solutions:
| Technique | How It Works |
|---|---|
| Stalling (bubbling) | Insert NOP (no operation) cycles until the required data is available. Simple but reduces performance |
| Forwarding (bypassing) | The result is sent directly from the ALU output of the producing instruction to the input of the consuming instruction, without waiting for it to be written back to the register file |
| Compiler reordering | The compiler rearranges instructions so that independent instructions fill the gap between dependent ones |
A control hazard (also called a branch hazard) occurs when the processor encounters a branch instruction and does not know which instruction to fetch next until the branch condition has been evaluated.
Example:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.