Save resource to class

Loading your classrooms…

Sign in to save

Bookmark this page so you can find it later.

LivePhysics staff accounts use their usual sign-in provider below.

Sign in to save

Bookmark this page so you can find it later.

LivePhysics staff accounts use their usual sign-in provider below.

Report an Error

Reported page · Tool

CPU Instruction Cycle Simulator

/tools/cpu-instruction-cycle-simulator/

This helps us reproduce what you saw. Clear the link to leave it out.

or drop them here

Up to 3 images or PDFs, 5 MB per file. 0 of 3 attached.

CPU Instruction Cycle Simulator

See how a processor runs a program. Write a short assembly listing for a small register machine, then step through each instruction as it moves through fetch, decode, and execute. The program counter, the four registers, the data memory, the zero flag, and the output console all update one phase at a time, so you can watch exactly how an instruction changes the machine. Everything runs in your browser and is fully deterministic.

Step 1 of 15FetchThe instruction at address 0 travels from program memory to the instruction register.
CPUProgram memory0MOV R0, #51MOV R1, #72ADD R0, R13OUT R04HALTControl unitwaiting for decodeFetchDecodeExecuteProgram counter is 0Program counter0Instruction register holds MOV R0, #5Instruction registerMOV R0, #5R0 holds 0R00R1 holds 0R10R2 holds 0R20R3 holds 0R30Zero flag is 0Z flag0Output is emptyOutputemptyData memoryMemory cell 0 holds 000Memory cell 1 holds 010Memory cell 2 holds 020Memory cell 3 holds 030Memory cell 4 holds 040Memory cell 5 holds 050Memory cell 6 holds 060Memory cell 7 holds 070Memory cell 8 holds 080Memory cell 9 holds 090Memory cell 10 holds 0100Memory cell 11 holds 0110Memory cell 12 holds 0120Memory cell 13 holds 0130Memory cell 14 holds 0140Memory cell 15 holds 0150MOV R0, #5Step 1 of 15

Drag the step slide or tap Fetch, Decode or Execute. The arrow marks the program counter and amber marks the part used in this step.

Go to step (of 15)
Sample programs
Output

No output yet. OUT appends a register value here.

One instruction per line. Use a semicolon for comments and a name ending in a colon for a label. Editing the program rebuilds the trace and returns to the first step.

Reference Guide

The fetch, decode, execute cycle

A CPU runs a program one instruction at a time, and each instruction passes through three phases.

Fetch. The control unit reads the next instruction from the program into the instruction register, using the program counter to know where to look.

Decode. The instruction is interpreted to find the operation and its operands.

Execute. The operation is carried out, updating registers, memory, or flags, and the program counter advances to the next instruction unless a jump changes it.

Program counter and instruction register

Program counter (PC). Holds the index of the next instruction to fetch. After a normal instruction it moves forward by one, so the machine reads top to bottom.

Instruction register (IR). Holds the raw instruction that was just fetched while it is being decoded and executed.

A jump instruction works by writing a new value into the program counter, which is how loops and branches change the order of execution.

Registers versus memory

Registers. This machine has four general purpose registers, R0 through R3, plus the program counter and the zero flag. Registers are small and fast and hold the values a program is actively working with.

Data memory. There are 16 numbered cells, addressed 0 to 15, used for longer term storage. Values move between memory and registers with LOAD and STORE.

Real processors keep this split for the same reason, registers are quick to reach while main memory is larger but slower.

The instruction set

The toy CPU understands a small set of operations.

MOV   Rd, #n   or  MOV Rd, Rs
LOAD  Rd, [addr]
STORE [addr], Rs
ADD   Rd, Rs   or  ADD Rd, #n
SUB   Rd, Rs   or  SUB Rd, #n
CMP   Ra, Rb   or  CMP Ra, #n
JMP / JZ / JNZ  label
OUT   Rs
HALT

An operand starting with a hash is an immediate number, square brackets name a memory address, and a bare name like R0 is a register.

Labels, jumps, and branching

A label is a name ending in a colon that marks a position in the program. A jump instruction sends the program counter to that position.

JMP. Always jumps to the label, which is how you build a loop.

JZ and JNZ. Jump only when the zero flag is set or clear, which is how you build a decision. Together with CMP they let a program repeat a block a fixed number of times or stop when a value reaches a target.

The zero flag and what this is

Zero flag (Z). A single bit that CMP sets to true when its two operands are equal and to false when they differ. Conditional jumps read this flag to decide whether to branch.

This is a teaching model, not a real processor. There is no pipeline, cache, or interrupt handling, and it works with plain integers only. A step limit stops runaway loops so the page never hangs. Everything runs in your browser, so nothing is sent to a server.

Related Content