Sub topic 3
The Five-Stage Pipeline: IF, ID, EX, MEM, WB
Assume you know the basics of CPU registers, memory, clock cycles, and assembly instructions such as add, lw, sw, and conditional branches. This chapter uses the classic five-stage pipeline found in many RISC processor models.
A pipeline divides instruction execution into stages. Each stage performs part of the work during one clock cycle. Different instructions can occupy different stages at the same time. This overlap improves instruction throughput, although one instruction still passes through all five stages.
The five stages
| Stage | Name | Main work |
|---|---|---|
| IF | Instruction Fetch | Read the instruction from memory |
| ID | Instruction Decode | Decode it and read source registers |
| EX | Execute | Perform an ALU operation or calculate an address |
| MEM | Memory access | Read from or write to data memory |
| WB | Write back | Store the result in the destination register |
IF: Instruction Fetch
The program counter, or PC, holds the address of the next instruction. During IF, the processor uses the PC to read an instruction from instruction memory. It also usually calculates the address of the next sequential instruction.
For example, if instructions occupy four bytes, the next PC is commonly:
next PC = current PC + 4The fetched instruction and the next-PC information are stored in a pipeline register before the next stage begins.
ID: Instruction Decode
During ID, the control unit interprets the instruction fields. It identifies the operation, source registers, destination register, and any immediate constant.
The register file is read in this stage. For example:
add r1, r2, r3The processor reads r2 and r3, because the instruction will add their values in EX. The control unit also creates signals that tell later stages what to do.
EX: Execute
The EX stage performs the instruction's main computation. An arithmetic instruction uses the ALU to calculate a result. A load or store uses the ALU to calculate an effective memory address.
A branch also commonly compares operands and calculates its target address in EX. The exact branch stage depends on the processor design, so always use the stage stated in a problem.
MEM: Memory access
MEM accesses data memory for load and store instructions.
lw r1, 0(r2)reads data from the calculated address.sw r1, 0(r2)writes the value ofr1to that address.
For an arithmetic instruction, MEM usually performs no useful memory operation. The ALU result simply passes through this stage.
WB: Write back
WB writes a result into the destination register. An arithmetic instruction writes its ALU result. A load writes the value obtained from data memory.
Stores do not write a register, so they have no useful WB action.
How instruction types use the stages
| Instruction | IF | ID | EX | MEM | WB |
|---|---|---|---|---|---|
| `add` | Fetch | Read operands | Add | Pass result | Write ALU result |
| `lw` | Fetch | Read base register | Calculate address | Read memory | Write loaded value |
| `sw` | Fetch | Read base and data | Calculate address | Write memory | No register write |
The five-stage model is an abstraction. Real processors may split stages, combine stages, or use different names. For GATE problems, follow the stated model and identify what each instruction needs in every stage.
Exercise
In which stage does lw r1, 8(r2) calculate the memory address, and in which stage does it obtain the loaded data?
Show answer
It calculates the address in EX and obtains the data from memory in MEM.