AQA 7517, section 4.7.3.2
The processor runs a program by repeating the fetch-execute cycle (also called the fetch-decode-execute cycle) over and over, once for each instruction.
Stage 1: Fetch
The next instruction is brought from main memory into the processor.
- The address in the program counter (PC) is copied to the memory address register (MAR).
- The PC is incremented to point to the next instruction.
- The instruction at that address is read from memory (the address goes on the address bus, a read signal on the control bus, and the instruction comes back on the data bus) into the memory buffer register (MBR).
- The instruction is copied from the MBR into the current instruction register (CIR).
In register-transfer notation:
MAR <- PC
PC <- PC + 1
MBR <- memory[MAR]
CIR <- MBR
Stage 2: Decode
The control unit looks at the instruction in the CIR. It splits it into the opcode (which operation) and the operand (the value, register or address to use), works out what the instruction means, and prepares the control signals needed to carry it out.
Stage 3: Execute
The instruction is carried out. What happens depends on the instruction.
- Arithmetic or logic: the ALU does the calculation using registers.
- Load: the operand address goes to the MAR, the data is read into the MBR, and copied into a register.
- Store: a register value is placed in the MBR, the address in the MAR, and the value is written to memory.
- Branch: the PC is changed to a new address, so the next fetch comes from there.
The status register may be updated, and then the cycle starts again with the next fetch.
Registers involved
| Register | Used in the cycle to... |
|---|---|
| PC | Hold the address of the next instruction, and be incremented during fetch |
| MAR | Hold the address being read from or written to in memory |
| MBR | Hold the instruction or data that has just been read, or is about to be written |
| CIR | Hold the instruction while it is decoded and executed |
| General-purpose registers / accumulator | Hold the data being worked on and the results |
| Status register | Record flags after an operation (zero, negative, carry, overflow) |
Worked example: a traced program
A simple machine has this program in memory, with the data values:
| Address | Contents |
|---|---|
| 0 | LOAD 10 (load the value at address 10 into the accumulator) |
| 1 | ADD 11 (add the value at address 11) |
| 2 | STORE 12 (store the accumulator at address 12) |
| 3 | HALT |
| 10 | 5 |
| 11 | 7 |
| 12 | (empty) |
The PC starts at 0. Trace the registers:
| Step | PC | MAR | MBR | CIR | ACC |
|---|---|---|---|---|---|
| Start | 0 | ||||
| Fetch 1: MAR from PC, PC incremented | 1 | 0 | LOAD 10 | LOAD 10 | |
| Execute LOAD: address 10 to MAR, read | 1 | 10 | 5 | LOAD 10 | 5 |
| Fetch 2 | 2 | 1 | ADD 11 | ADD 11 | 5 |
| Execute ADD: address 11 to MAR, read, add | 2 | 11 | 7 | ADD 11 | 12 |
| Fetch 3 | 3 | 2 | STORE 12 | STORE 12 | 12 |
| Execute STORE: ACC to MBR, written to address 12 | 3 | 12 | 12 | STORE 12 | 12 |
| Fetch 4 | 4 | 3 | HALT | HALT | 12 |
| Execute HALT | 12 |
At the end, address 12 holds 12 (). Notice that the PC is always one ahead of the instruction being executed, because it is incremented during the fetch.
Exam tip: in a trace, show the PC being incremented in the fetch stage, before the instruction is executed. A branch instruction works by overwriting the PC. Use the correct registers for each transfer, such as address into the MAR and data in the MBR.
Check yourself
Name the three stages of the cycle
Fetch, decode and execute.
What happens to the PC during the fetch stage?
Its value is copied to the MAR, and then it is incremented so it points at the next instruction.
How does a branch instruction change the normal sequence?
It puts a new address into the program counter, so the next instruction is fetched from there instead of the next one in order.
In which register is the instruction held while it is being decoded?
The current instruction register (CIR).