Crux Trace small programs in the toy instruction set, predict register and program-counter state, decode a machine-code word, and count clock cycles.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at middle altitude — in the sky
◷ 14 min
Understanding the processor means being able to run it in your head. Each snippet below is a tiny program or a single machine-code word; read it, step the loop, and predict the state the way the CPU would produce it.
Goal
Practise the core skill of the unit: take instructions plus an initial state, step the fetch-decode-execute loop by hand, and read off the resulting register values, program-counter value, machine-code meaning, and cycle count.
These snippets use the toy instruction set from the unit: each instruction is 2 bytes, the program counter advances by 2 after every non-JUMP instruction, and the registers are R0 and R1.
After all four instructions execute, what value is stored in memory address 202?
Heads-up STORE 202, R0 writes the value held in register R0, not the contents of some memory address. By then R0 holds the sum 13, not 9.
Heads-up The only arithmetic instruction here is ADD, which adds R0 and R1. There is no subtraction; 4 + 9 = 13.
Heads-up R0 is loaded from mem[201] = 4 in the first instruction, so its initial 0 is overwritten before the ADD. The result is fully determined: 13.
Snippet 2 — the program counter and a jump
; Initial state: PC = 0, R0 = 0; Each instruction is 2 bytes.0: LOAD R0, 2002: JUMP 64: ADD R0, R1 ; never reached6: STORE 202, R0
Quiz
Completed
Which instruction at address 4 does NOT run, and what is the program-counter value immediately after the JUMP at address 2 executes?
Heads-up A JUMP overrides the normal +2 advance. It writes its target (6) into the PC, so address 4 is stepped over entirely.
Heads-up The PC is set to the jump target 6, not advanced past it. Address 4 is skipped, and the next fetch is at 6.
Heads-up PC = 6 is correct, but that means the fetch at address 6 (STORE) does run. The instruction stepped over is the ADD at address 4.
Snippet 3 — decoding a machine-code word
; 16-bit instruction encoding:; bits 15-12 (4 bits) = opcode; bits 11-8 (4 bits) = destination register number; bits 7-0 (8 bits) = immediate value; opcode 0001 = LOAD-IMMEDIATE (put the constant into the register)Word in memory: 0001 0011 00010010
Quiz
Completed
Decoding this 16-bit word, which instruction does it represent?
Heads-up The immediate field is an 8-bit binary value, not hex. Binary 00010010 = 16 + 2 = 18, not 12.
Heads-up The opcode is the top 4 bits, 0001 (LOAD-IMMEDIATE). 0011 is the destination-register field, which names register 3, not an opcode.
Heads-up Whether bytes are an instruction is set by the program counter, not by the bytes. Given that the PC points here, the fields decode unambiguously: LOAD-IMMEDIATE, register 3, value 18.
Snippet 4 — counting clock cycles
; A program runs 6 instructions in a tight loop body.; On this CPU every instruction takes exactly 1 clock cycle.; The loop body runs 100 times. The CPU clock runs at 2 GHz; (2,000,000,000 clock cycles per second).
Quiz
Completed
How many clock cycles does the loop body consume in total, and roughly how long does that take at 2 GHz?
Heads-up Each iteration re-runs the loop body, so the cycles multiply by the iteration count: 6 x 100 = 600, not 6.
Heads-up The cycle count is right but the time is off by a million. 600 cycles / 2e9 cycles per second = 3e-7 s = 300 nanoseconds, not milliseconds.
Heads-up Each iteration runs all 6 instructions, and each instruction is 1 cycle, so an iteration costs 6 cycles. 100 iterations x 6 = 600.
Recap
Every question here was answered by stepping the same loop: fetch the instruction the PC points to, decode its opcode and operands, execute it (LOAD and STORE move data between memory and registers; ADD uses the ALU on register values; JUMP rewrites the PC), then advance the PC by the instruction size — unless a JUMP set it directly. Machine code decodes by splitting the word into opcode and operand fields. And run time is just instructions x iterations x cycles-per-instruction, divided by the clock rate. Trace it by hand and the processor stops being a black box.