CPU simulator
A CPU simulator shows what a processor does, one step at a time, as it runs a program. Our CPU Simulator runs a small assembly language based on Little Man Computer and AQA mnemonics, and steps through the fetch-decode-execute cycle in register transfer notation, lighting up the program counter, MAR, MDR, CIR, accumulator and the buses as each one is used.
It opens on a short program that adds two numbers held in memory, 25 and 17, and outputs 42.
CPU Simulator
Step through fetch, decode and execute in assembly
How to use the CPU simulator
- Under Program, type an assembly program, or choose one from Examples: add two numbers, count down, the larger of two inputs, multiplication, a sum from 1 to n, Fibonacci numbers, masks and shifts, or running into data.
- If the program uses INP, type its inputs in Inputs, for INP.
- Errors are reported against each line, such as a missing label or a value out of range.
- Under Run, step forwards and backwards through the run, one register transfer at a time.
- Watch the registers, the memory grid and the address, data and control buses light up, and read the note under each step. Show memory in decimal or hex.
The Instructions panel lists every mnemonic. Write #5 for the number 5, or a label or address for what is in memory there; labels end in a colon and comments start with a semicolon.
The instruction set
| Mnemonic | What it does |
|---|---|
| LDA | Load a value into the accumulator (ACC) |
| STA | Store ACC at an address |
| ADD, SUB | Add to or subtract from ACC |
| AND, ORR, EOR | Bitwise AND, OR, XOR with ACC |
| LSL, LSR | Shift ACC left or right |
| INP, OUT | Read the next input into ACC, or send ACC to the output |
| BRA, BRZ, BRP | Branch always, if ACC is zero, or if ACC is zero or positive |
| DAT | Store a number in memory |
| NOP, HLT | Do nothing, or stop |
Memory has 64 addresses, and each holds a whole number from −32,768 to 32,767. An instruction is one word: the opcode times 256 plus the operand, so LDA 5 is stored as 1 × 256 + 5 = 261.
The fetch-decode-execute cycle
Step through the first instruction of the opening program, LDA first, and the simulator shows each register transfer:
| Phase | Register transfer | What happens |
|---|---|---|
| Fetch | MAR ← [PC] | The address of the next instruction goes to the memory address register |
| Fetch | MDR ← [[MAR]] | Memory puts the instruction on the data bus, into the memory data register |
| Fetch | PC ← [PC] + 1 | The program counter moves on to the next address |
| Fetch | CIR ← [MDR] | The instruction goes to the current instruction register |
| Decode | Decode LDA 5 | The control unit reads the opcode |
| Execute | MAR ← 5 | The operand address goes to the MAR |
| Execute | MDR ← [5] | Memory puts 25 on the data bus |
| Execute | ACC ← [MDR] | 25 goes into the accumulator |
Square brackets mean "the contents of". The whole five-instruction program takes 37 steps, including the start, and outputs 25 + 17 = 42.
Check yourself:
Example programs to try
Count down
A loop that outputs 5, 4, 3, 2, 1, 0: it outputs the accumulator, uses BRZ to stop once it reaches zero, subtracts 1 with SUB #1, and uses BRA to jump back. Watch the branch change the program counter, so execution jumps back instead of moving on.
Running into data
A program with no HLT runs on into its data, and the CPU tries to execute a number as an instruction. This is why every program needs a HLT before its DAT lines; if the program counter runs past the last address, the simulator stops with a fault and asks whether a HLT was missed.
What the simulator does not do
- One accumulator. There are no general-purpose registers R0 to R12, so programs written for the full AQA instruction set with registers need rewriting.
- No stack, interrupts or subroutines. The machine has branches only.
- No pipelining or cache. It runs one instruction at a time, to show the cycle clearly.
Common mistakes
- Mixing up the MAR and the MDR. The MAR holds an address; the MDR holds the data read from or written to it. AQA calls the MDR the memory buffer register (MBR).
- Forgetting the PC increments during fetch. It already points to the next instruction while the current one executes.
- Confusing #5 with 5. #5 is the number 5; plain 5 means the contents of address 5.
- Leaving out HLT. Without it, the CPU runs on into the data.
Practise computer systems in a short timed paper
The CPU and the fetch-decode-execute cycle come up in GCSE Computer Science and A-level Computer Science practice. Each daily paper is a short timed paper of 8 questions worth 25 to 35 marks, and the papers mix topics. Our how does a CPU work guide explains the parts of the processor, and the PC Components course covers cores and clock speed.
Common questions
What is a CPU simulator?
What happens in the fetch-decode-execute cycle?
What do the MAR and MDR do?
Is this a Little Man Computer simulator?
What is register transfer notation?
What does the accumulator do?
Revision cards
Say the answer first, then flip the card to check it.
Where to go next
- Solve it
CPU Simulator
Step through fetch, decode and execute in assembly
Open the tool - Learn it
PC Components
CPUs, GPUs and RAM: understand what makes a computer tick
Start the course - Test it
GCSE Computer Science
A new exam-style paper every day, with instant marking and a mark scheme
Start today's paper