OCR H446, section 1.1.1 (b)
The processor runs a program by repeating the fetch-decode-execute (FDE) cycle over and over, once for each instruction. You need to know what happens at each stage, and how the registers change.
Stage 1: Fetch
The next instruction is brought from memory into the processor.
- The address in the PC is copied to the MAR.
- The PC is incremented, so it points to the next instruction.
- The instruction at that address is read from memory (address on the address bus, read signal on the control bus, instruction returns on the data bus) into the MDR.
- The instruction is copied from the MDR into the CIR.
MAR <- PC
PC <- PC + 1
MDR <- memory[MAR]
CIR <- MDR
Stage 2: Decode
The control unit examines the instruction in the CIR. It splits it into an opcode (what to do) and an operand (the address or value to use), works out what it means, and prepares the control signals needed.
Stage 3: Execute
The instruction is carried out. What happens depends on the instruction. For example, for load, store and add instructions:
| Instruction | What happens to the registers |
|---|---|
LDA x (load) |
MAR gets the operand address, MDR receives the data from memory, which is copied to the ACC |
STA x (store) |
MAR gets the operand address, the ACC is copied to the MDR, which is written to memory |
ADD x |
MAR gets the address, MDR receives the data, and the ALU adds it to the ACC (result in the ACC) |
SUB x |
The same, but the ALU subtracts |
BRA x (branch always) |
The PC is overwritten with the operand, so the next fetch is from there |
BRZ x (branch if zero) |
The PC is overwritten only if the ACC is zero |
BRP x (branch if positive) |
The PC is overwritten only if the ACC is positive (or zero) |
INP / OUT |
A value moves between the input or output and the ACC |
HLT |
The cycle stops |
A branch is the only kind of instruction that changes the PC in the execute stage. Everything else just lets the PC carry on from the value it was given in the fetch.
What changes in each register
| Register | When it changes |
|---|---|
| PC | Incremented in fetch. Overwritten by a branch in execute |
| MAR | Gets the PC in fetch, and an operand address in execute (for load, store, add and so on) |
| MDR | Gets the instruction in fetch, and data in execute |
| CIR | Gets the instruction in fetch. Holds it through decode and execute |
| ACC | Changed by load, add, subtract and input (in execute) |
A traced example
A short program (load, add, store) and its data are in memory:
| Address | Contents |
|---|---|
| 00 | LDA 10 |
| 01 | ADD 11 |
| 02 | STA 12 |
| 03 | HLT |
| 10 | 7 |
| 11 | 5 |
| 12 | 0 |
The PC starts at 0.
| Step | PC | MAR | MDR | CIR | ACC |
|---|---|---|---|---|---|
| Start | 0 | ||||
| Fetch 1 | 1 | 0 | LDA 10 | LDA 10 | |
Execute LDA 10 |
1 | 10 | 7 | LDA 10 | 7 |
| Fetch 2 | 2 | 1 | ADD 11 | ADD 11 | 7 |
Execute ADD 11 |
2 | 11 | 5 | ADD 11 | 12 |
| Fetch 3 | 3 | 2 | STA 12 | STA 12 | 12 |
Execute STA 12 |
3 | 12 | 12 | STA 12 | 12 |
| Fetch 4 | 4 | 3 | HLT | HLT | 12 |
Execute HLT |
12 |
At the end, memory location 12 holds 12 (). The PC is always one ahead of the instruction being executed, because it is incremented during the fetch.
Interrupts
At the end of each cycle, the processor checks for interrupts. If one is waiting, the current state is saved and the interrupt service routine runs (see 1.2.1 (c)).
Worked example
Task: show the register changes for the fetch stage when the PC holds 14, and address 14 holds the instruction SUB 30.
| Step | PC | MAR | MDR | CIR |
|---|---|---|---|---|
| Before | 14 | |||
MAR <- PC |
14 | 14 | ||
PC <- PC + 1 |
15 | 14 | ||
MDR <- memory[MAR] |
15 | 14 | SUB 30 | |
CIR <- MDR |
15 | 14 | SUB 30 | SUB 30 |
After the fetch, the CIR holds SUB 30 and the PC points to address 15.
Exam tip: show the PC being incremented during the fetch, before the instruction is executed. Use the correct register for each transfer (address into MAR, data in MDR), and remember that only a branch changes the PC during execute.
Check yourself
What are 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 that it points at the next instruction.
How does a BRA instruction affect the registers?
It overwrites the PC with the operand, so the next instruction is fetched from the branch address.
In which register is an instruction held while it is decoded?
The current instruction register (CIR).