Sub topic 1
Recap: the instruction cycle
This chapter reviews how a processor executes one instruction at a time. Pipelining overlaps these steps, so you need a precise picture of the steps before you can overlap them. The chapter also fixes the vocabulary and the timing arithmetic that the later chapters use.
Prerequisites
This chapter assumes you already know the following:
- Basic computer organization: the CPU, registers, memory, and the clock.
- Simple assembly-style instructions such as load, store, add, and branch.
- Ratio arithmetic, such as computing a speedup as old time divided by new time.
If any of these is unfamiliar, review it first. The rest of the tutorial builds directly on them.
What the instruction cycle is
A processor runs a program by repeating one loop for every instruction. This loop is the instruction cycle, also called the fetch-decode-execute cycle. Each pass handles exactly one instruction and ends with the processor ready to start the next one.
The program counter (PC) is a register that holds the memory address of the next instruction to run. The instruction register (IR) holds the instruction currently being worked on. These two registers connect the steps of the cycle.
The three basic steps
- Fetch. The processor sends the PC value to instruction memory and reads the instruction into the IR. It then advances the PC to the next sequential address, which is PC + 4 for fixed 4-byte instructions.
- Decode. The control logic reads the opcode and works out what the instruction does. It reads the source registers named in the instruction. It may also extend a short immediate value to full word width (sign extension).
- Execute. The processor carries out the operation. Depending on the instruction, this means an ALU calculation, a memory address calculation, a memory access, a branch decision, or a write to a register.
Textbooks differ on how finely they split the third step. Some keep "execute" as one step. Others separate out memory access and register write-back. The five-stage pipeline in the next chapters uses the finer split.
Splitting execute into finer steps
A load instruction shows why the execute step is often split further. Consider lw r1, 0(r2). It needs these actions in order:
- Fetch the instruction.
- Decode it and read register
r2. - Add 0 to
r2in the ALU to form the address. - Read data memory at that address.
- Write the loaded value into
r1.
An add instruction such as add r3, r1, r4 needs no data memory access. A store such as sw r1, 0(r2) needs the memory access but writes no register. Instruction types therefore use different subsets of the same five actions. This variation matters later, because a pipeline must treat every instruction in a uniform way even if it skips some actions.
The table shows which actions each common instruction type uses.
| Instruction type | Fetch | Decode and read registers | ALU | Data memory | Register write |
|---|---|---|---|---|---|
| ALU (add, sub) | yes | yes | computes result | no | yes |
| Load (lw) | yes | yes | computes address | read | yes |
| Store (sw) | yes | yes | computes address | write | no |
| Branch (beq) | yes | yes | compares or computes target | no | no |
Single-cycle versus multi-cycle execution
A processor can implement the instruction cycle in two basic ways. Both are sequential: the next instruction starts only after the current one has finished.
- Single-cycle design. Every instruction finishes in exactly one clock cycle. The cycle time must cover the slowest instruction, which is usually the load, because it uses every action. Faster instructions waste the rest of their cycle. The cycles per instruction (CPI) is 1.
- Multi-cycle design. Each step takes one short clock cycle, and an instruction uses as many cycles as it needs. An add may take 4 cycles and a load 5. The clock is shorter, but CPI is greater than 1. Hardware units are idle for much of the time, because only one step is active at once.
In both designs the hardware for the other steps sits unused while one step runs. This idle hardware is the waste that pipelining removes.
Timing arithmetic for the sequential case
Let the steps take these times, in nanoseconds: fetch 60, decode 80, execute 70, memory 90, write-back 50. The sum is 350 ns.
- In a single-cycle design, the clock period is at least 350 ns for a load that uses all steps. Running n instructions takes n × 350 ns.
- In a multi-cycle design with a clock set by the slowest step, the period is 90 ns. A five-step instruction takes 5 × 90 = 450 ns, which is worse than the single-cycle total for that instruction. The multi-cycle design gains only when many instructions use fewer than five steps.
The next code block computes the single-cycle total for a program. It is the baseline against which the pipeline speedup is measured in later chapters.
def sequential_time(step_delays, n):
# Each instruction runs all steps one after another,
# and the next instruction starts only after it finishes.
return n * sum(step_delays)
delays = [60, 80, 70, 90, 50] # ns per step
print(sequential_time(delays, 1)) # 350
print(sequential_time(delays, 100)) # 35000This 35000 ns figure for 100 instructions is the same non-pipelined time you will meet again when pipeline speedup is computed.
Where the instruction cycle breaks down
The cycle assumes instructions run in program order, one at a time. That assumption makes the processor's behavior easy to reason about, and a pipeline must preserve it. A few points deserve care:
- Branches change the PC. After fetch, the PC points to the next sequential instruction. A taken branch overwrites it with the target address. The processor learns the target only after decode or execute, so the sequential fetch may have been wrong.
- Instructions share state. One instruction writes a register and the next reads it. In sequential execution the write always finishes first, so the read sees the new value. Overlap puts this guarantee at risk.
- Memory is a shared resource. Instruction fetch and data access both use memory. A sequential design never needs both in the same cycle, but an overlapped design may.
Each of these three points becomes a hazard class later: control, data, and structural.
Why this leads to pipelining
The processor has separate hardware for each step: an instruction memory port, a register file, an ALU, a data memory port, and a write port. In sequential execution, only one of these is busy at any moment. While the ALU works on instruction 1, the instruction fetch unit sits idle even though instruction 2 could already be fetched. Pipelining starts the next instruction's fetch while the current instruction is being decoded. Throughput rises because all units work at once, though each single instruction takes no less time.
Summary of terms
- Instruction cycle: the repeated fetch, decode, execute loop that handles one instruction per pass.
- PC: the register holding the next instruction address.
- IR: the register holding the current instruction.
- CPI: average clock cycles per instruction.
- Single-cycle design: one long clock cycle per instruction, so CPI is 1.
- Multi-cycle design: one short clock cycle per step, so CPI is above 1.
Exercise
A machine has five steps with delays 60, 80, 70, 90, and 50 ns. It is a single-cycle design, so each instruction takes one clock cycle that covers all five steps. How long does it take to run 20 instructions, and what is the CPI?
Show answer
The clock period must cover all five steps, so it is 60 + 80 + 70 + 90 + 50 = 350 ns. Twenty instructions take 20 × 350 = 7000 ns. The CPI is 1, because each instruction completes in one clock cycle.
Exercise
Name the three properties of sequential execution that overlapping instructions puts at risk, and give the hazard class each one leads to.
Show answer
First, the PC can be changed by a branch after the next instruction has been fetched, which leads to control hazards. Second, one instruction may write a register that the next one reads, which leads to data hazards. Third, fetch and data access may need memory in the same cycle, which leads to structural hazards.