Week 3 · Lesson 2 · 55 minutes

Fetch decode and execute

I can trace the registers while a simple program runs.

Planned lesson. Follow your teacher’s teaching order.

Source workbook pages: 19.

Resources

Vocabulary

Vocabulary
fetch: get the next instruction from memory; decode: work out which operation an instruction requests
55-minute lesson sequence
  1. 0–5 · Retrieve PC.
  2. 5–15 · Act out the LOAD example with instruction and data cards.
  3. 15–25 · Guide the fetch part of task 1; stop before showing the sum.
  4. 25–45 · Students finish ADD and STORE traces and draw the repeating cycle.
  5. 45–50 · Exchange traces and check instruction addresses against operand addresses.
  6. 50–55 · Collect the fetch/execute distinction.

See it

Fetch, decode and execute LOAD 10FetchMAR ← PCMDR ← memory[MAR]IR ← MDRPC ← PC + 1DecodeLOAD 10ExecuteMDR ← memory[10]AC ← MDRthen fetch the next instructionMemory: address 0 holds LOAD 10address 10 holds 7End: PC = 1, AC = 7
Fetch always runs the same four steps. Decode works out the operation. Execute depends on the instruction: here LOAD 10 copies the value at address 10 into AC.

Watch

Watch the cycle run on a toy machine with a different instruction set. The video opens with a short sponsor message.

The Fetch-Execute Cycle: What's Your Computer Actually Doing?Tom Scott

Open on YouTube

The video is on YouTube, which needs a VPN on most networks in mainland China. It is extra: the lesson works without it.

Try before notes

Which register holds the address of the next instruction?

Build the idea

Fetch obtains the next instruction; decode identifies its operation; execute performs that operation. The same cycle repeats even when different instructions have very different execution steps.

In this toy machine every instruction occupies one address. Fetch copies PC to MAR, reads memory into MDR, copies MDR to IR and increments PC by one. Real instruction encodings and processor designs vary.

When an instruction reads an operand from memory, MAR and MDR are used again during execution. Their final values may therefore differ from their values at the end of fetch. Always label the stage of a register snapshot.

PC → MAR → memory read → MDR → IR
PC ← PC + 1
CU decodes IR → execute → repeat

See the method

Our toy machine has one instruction per address. Memory 0 contains LOAD 10; memory 10 holds 7. Initially PC=0. Fetch: MAR←PC; MDR←memory[MAR]; IR←MDR; PC←PC+1. Decode LOAD 10. Execute: read memory[10] into MDR and copy it to AC. End state: PC=1, AC=7. The left arrow means assign.

Predict then reveal

Toy memory: [8]=LOAD 31 and [31]=14; PC=8. Trace fetch and execute. LOAD uses a memory address operand.

Reveal reasoning

Fetch: MAR=8; MDR="LOAD 31"; IR="LOAD 31"; PC=9. Execute: MAR=31; MDR=14; AC=14. PC remains 9.

Next, [9]=ADD 32 and [32]=6. What changes through its execution?

Reveal reasoning

Fetch sets IR to ADD 32 and PC to 10. Execute reads address 32: MAR=32, MDR=6. ALU adds AC 14 + 6, giving AC=20. ADD here uses memory contents, not the address number 32.

Work one step together

Read the first task. Each learner predicts the first step privately. Compare the method, then complete one intermediate row or one code iteration together. Do not display a finished workbook answer.

Practice

  1. Continue the model. Memory 1 is ADD 11; memory 11 holds 5. Trace PC, MAR, MDR, IR and AC through fetch and execute. What is the final result?

Your turn

Complete the remaining steps of task 1 alone, then the next tasks. Predict before running code. Save your own evidence before comparing.

Practice

  1. Continue the model. Memory 1 is ADD 11; memory 11 holds 5. Trace PC, MAR, MDR, IR and AC through fetch and execute. What is the final result?
  2. Memory 2 is STORE 12. Explain the write, including bus directions and the final PC. Draw the repeating fetch → decode → execute cycle.

Check and explain

The operand can mean an address or an immediate value depending on the declared machine. State the convention.

Point to the first step, row, condition or requirement that supports your answer. If results differ, find the first different state and repair one cause.

Show what you know

Why is fetching an instruction different from executing it?

HL challenge when ready

If a jump sets PC to 0, where will the next fetch read?

Week 3 homework

Show working. Keep code and output. Stop at 25 minutes and mark where you reached.

  1. In the toy machine PC=4. Memory 4 contains LOAD 20 and memory 20 holds 9. Trace the fetch and execute.
  2. Explain why cache, RAM and SSD are not interchangeable. Use one job for each.
  3. Six independent instructions use F,D,E,W with no stalls. Find the ideal cycles and compare with no overlap. Write ideal_cycles(n) in Python for n >= 1. Test n = 1, 4 and 6.
  4. Retrieve logic: simplify (A AND B) OR (A AND NOT B). Give one proof step.

Source workbook pages: 23.

Weekly homework (Word)

Python practice · two short prompts

Spend about 3 minutes on each during practice time. Predict, run, change one thing, then explain. These prompts do not add homework.

A starter is runnable code to explore or complete. If the playground restores your saved code, the linked starter is selected in Example: use Load example when ready. Download your work before confirming replacement.

Prompt 1

Trace one fetch: read the instruction, then advance the program counter.

No imports needed.

Hint and starter code

The program counter (pc) records the next instruction address in this simplified model.

# The program counter (pc) records the next instruction address in this simplified model.
program = ["LOAD 2", "ADD 3", "STOP"]
pc = 0
instruction = program[pc]
pc += 1
print(instruction, pc)
Open this starter in Python playground →
Prompt 2

Model the effect of LOAD then ADD on an accumulator. Predict each printed value.

No imports needed.

Hint and starter code

An accumulator is a register holding a working result. This is a trace, not a CPU simulator.

# An accumulator is a register holding a working result.
accumulator = 0
accumulator = 2
print(accumulator)
accumulator += 3
print(accumulator)
Open this starter in Python playground →