Proof Academy · Python · Journey 11

From Python to the processor

Step through a stored program, track CPU registers and explain how the runtime executes your code.

Lab and core lesson: 45–55 minutes, plus guided study · Builds on Binary and text encoding

01 · Understand and predict

Fetch, decode and execute a stored instruction

In the stored-program model, instructions and data occupy addressable memory. The control unit coordinates instruction processing. The arithmetic logic unit (ALU) performs arithmetic and logical operations. Registers are small, fast storage locations inside the processor.

The program counter (PC) holds the address of the next instruction. The memory address register (MAR) holds the address being accessed. The memory data register (MDR) holds the data being transferred. The current instruction register (CIR) holds the instruction being decoded. This model uses an accumulator (ACC) for working values.

During fetch, the CPU copies the PC to the MAR, reads the instruction through the MDR, and copies it to the CIR. The PC advances. During decode the control unit identifies the operation and operand. During execute the CPU performs the operation, which can involve another memory access or a change to the PC. The address bus carries locations, the data bus carries values or instructions, and control signals coordinate reads and writes.

Python source is high-level code. In a typical CPython run it is compiled to bytecode, which the Python runtime executes. Bytecode is an instruction format for the runtime. The CPU executes native machine instructions. In this browser lab Pyodide runs Python through WebAssembly. RAM holds working data, secondary storage holds saved files, and the operating system manages resources such as processes and files.

# A Python model of three data cells
memory = [4, 7, 0]
acc = memory[0]  # LOAD 0
acc = acc + memory[1]  # ADD 1
memory[2] = acc  # STORE 2
print(memory[2])

Worked trace

  1. LOAD copies the value 4 into the accumulator.
  2. ADD reads 7 from memory and makes the accumulator 11.
  3. STORE copies 11 into the destination cell. The source data cells still hold 4 and 7.

Step through the processor

This simplified machine stores its instructions at addresses 0–3 and its data at 6–8. Predict what each step changes, then advance the model.

Ready
The control unit prepares to fetch the instruction at address 0.

PC
0
MAR
—
MDR
—
CIR
—
ACC
0
0
LOAD 6
1
ADD 7
2
STORE 8
3
HALT
6
4
7
7
8
0
Which register supplies the address of the next instruction at the start of fetch?

From code to machine. The Python model shows the effects of three imagined instructions. A real processor implements its own instruction set, registers and timings. A clock coordinates steps, and the instruction’s operation determines which data moves and when.

Guided study

Read it, trace it, test it

Keep the levels of explanation separate

At the top level you write Python. The program might contain total = total + amount, which describes what should happen to two values. CPython typically compiles source to bytecode and runs it using the Python runtime. Bytecode is a format for that runtime, while the physical processor executes machine instructions from its own instruction set. In the browser this lab uses Pyodide and WebAssembly, so another runtime is involved. A Python variable or line is useful when tracing a program. A register or an instruction address is useful when tracing a processor. Do not assume one line corresponds to one machine instruction or one register.

Explain the stored-program model step by step

In the model on this page, main memory holds instructions and data. An address identifies a location; the value at that address can be an instruction or a piece of data. During fetch, the processor uses the program counter (PC) to identify the next instruction address. The address is copied to the memory address register (MAR). A memory read transfers the contents to the memory data register (MDR). The instruction is copied to the current instruction register (CIR) and the PC advances. The control unit then decodes the operation and any operand. Execution can involve the ALU, another memory access or a change to the PC.

| Moment | PC | MAR | CIR | ACC | What changed | | --- | --- | --- | --- | --- | --- | --- | | Before the first fetch | 0 | — | — | 0 | Next instruction is at address 0 | | After fetching LOAD 6 | 1 | 0 | LOAD 6 | 0 | Instruction read, PC advanced | | After executing LOAD 6 | 1 | 6 | LOAD 6 | 4 | Operand read from address 6 | | After executing ADD 7 | 2 | 7 | ADD 7 | 11 | ALU adds the value at address 7 | | After executing STORE 8 | 3 | 8 | STORE 8 | 11 | Address 8 now contains 11 |

Use Next step in the model and compare the registers with the table. After fetch, the MAR can still contain the instruction address, while the CIR holds the instruction that was read. After execution, the MAR can contain a data address. For STORE 8, the number 8 is an address, not the number stored. The data bus carries the value 11 in this example. The address bus identifies where it goes and the control signals request a write.

Explain how a loop changes the next instruction

The model shows straight-line execution until HALT. A branch instruction can set the PC to another address. For example, a conditional branch might return to an earlier instruction if a counter is above zero. Fetch then uses that earlier address. This is how the next instruction can differ from the next one in memory. A loop in Python has the same high-level idea, but it runs through the runtime. Avoid trying to map each Python statement directly onto the simple model's registers.

Place memory, storage and software in the picture

RAM holds the instructions and working data needed by running programs. It is normally volatile. Secondary storage, such as an SSD, keeps saved files when power is off. A cache stores copies of frequently or recently used data and instructions closer to the processor. The operating system manages resources such as memory, files, processes and devices. When you open a Python file in an IDE, the file may be on an SSD, the editor and runtime work with data in RAM, and the processor executes the instructions that carry out the work. Saving the script writes it to storage. Running it does not automatically save your edits or its output. In this browser lab, files made by Python are temporary unless you export what you want to keep.

Practise the distinction between a model and the real machine

Imagine a simplified program with LOAD 6, ADD 7, STORE 8 and HALT at addresses 0–3, but change the data to 10 at address 6 and −3 at address 7. Write the accumulator after each execute phase, the PC just after fetching STORE 8, and the value saved in address 8. Describe which part of the CPU performs the addition and what the control unit does when it decodes STORE. Then explain why tracing acc = acc + memory[1] in Python does not tell you exactly which registers a real CPU uses.

Explain performance with a workload in mind

A higher clock frequency can allow more cycles per second. More cores can help if work can run in parallel. A larger cache can reduce slower memory accesses if it keeps the data the program reuses. Explain the condition rather than assuming one specification determines performance.

Check your guided practice

After LOAD, ACC is 10. After ADD, ACC is 7. STORE leaves ACC at 7 and writes 7 to memory address 8. Fetching STORE 8 reads address 2 and advances the PC to 3. The ALU performs the addition. The control unit decodes the store operation and coordinates a write to the operand address. The Python line is carried out by the runtime, which executes machine instructions on a particular architecture. The teaching model deliberately simplifies that process.

02 · Run and repair

Store the result at the destination

This model takes two integers, loads the first and adds the second. Its store is supposed to write the result into cell 2. Find why the program prints 0 and repair the destination address.

Enter one value per line. Each input() reads the next line.

Ready to run.

Need a hint?

Only the store destination is wrong. Cell 2 is the third item in a Python list.

03 · Build independently

Model the accumulator

Create a teaching model that reads a count, followed by that many commands. Track the accumulator, a single memory cell and the number of instructions executed.

  • Start the accumulator and memory cell at zero. LOAD n replaces the accumulator. ADD n adds to it. STORE copies it into the memory cell.
  • Each command is valid and on its own line. HALT counts as an executed instruction, then ends the loop immediately. Commands after HALT are skipped.
  • Print the accumulator, memory cell and instruction count on three separate lines.

Enter one value per line. Each input() reads the next line.

Ready to run.

Need a hint?

Use split() to separate the operation from the optional value. Update the count before testing HALT. Use break to finish immediately.

04 · Explain and transfer

Explain your reasoning

Why can the accumulator and stored cell contain different values after an ADD?

Compare your explanation

ADD changes the accumulator. The memory cell retains the value from the most recent STORE. Another STORE is needed to copy the new accumulator value into memory.

Independent follow-up. Extend the teaching model with a conditional jump that changes a program counter. Use a list of commands and a maximum step count. Explain how a jump makes a loop possible and how your step limit prevents a mistaken loop from running forever.

Original GCSE practice · Pearson Edexcel 1CP2 3.1.1–3.1.2, 3.2.1 and 3.3.1–3.3.2. Download your .py file to keep it after leaving the page.

Next: Project: build a results analyser →