Lesson 68: Fetch, decode, execute
Inside the CPU
- The CPU repeats three steps: fetch an instruction from memory, decode it, execute it.
- Registers are tiny, fast stores inside the CPU. This sketch uses a variable for each one.
| Register | Holds |
|---|---|
| PC (program counter) | the address of the next instruction |
| MAR (memory address register) | the address to read from or write to |
| MDR (memory data register) | the data or instruction read from memory |
| CIR (current instruction register) | the instruction being decoded |
| ACC (accumulator) | the result of a calculation |
The program in memory
| Address | Contents | Meaning |
|---|---|---|
| 0 | 106 | LOAD from 6 |
| 1 | 207 | ADD from 7 |
| 2 | 308 | STORE to 8 |
| 3 | 900 | HALT |
| 6 | 5 | data |
| 7 | 7 | data |
In the exam
- 0478/12/M/J/24 Q5(a): complete the paragraph about fetching an instruction. The answers, in order: the PC holds the address of the next instruction; it is sent to the MAR on the address bus; then to RAM; the instruction at that address is sent to the MDR on the data bus; then to the CIR, which is in the control unit (CU).
Dificuldade 3/5 · Moedas 200 + pequeno bônus na primeira conclusão hoje
This sketch is a tiny CPU. memory holds a program, and the variables are its registers. Write the four steps of fetch(): the PC goes to the MAR, the instruction at that address goes to the MDR, the PC goes up by 1, and the MDR goes to the CIR. The sketch fetches the first 4 instructions and prints the registers each time.
Clique em Executar para ver a saída aqui.
Dificuldade 4/5 · Moedas 280 + pequeno bônus na primeira conclusão hoje
Agora decodificar e execute each instruction. 207 means opcode 2 (ADD) with address 7: CIR / 100 gives the opcode and CIR % 100 the address. LOAD is done. Add ADD, STORE and HALT, so the program works out 5 + 7 and stores 12 at address 8.
Clique em Executar para ver a saída aqui.