Write code for the machine you built
15A simple virtual processor
- Stage 1
- Stage 2
- Stage 3
- Stage 4
- Stage 5
- Stage 6
- Stage 7
- Stage 8
- Stage 9
- Stage 10
- Stage 11
- Stage 12
- Stage 13
- Stage 14
- Stage 15 (this one)
Explanation
Everything joins up: registers, an ALU with flags, a decoder, memory, and a control unit stepping through fetch, decode and execute. Bench-8 is a conceptual 8-bit accumulator machine with 16 instructions. Write assembly, assemble it into bytes, then run it cycle by cycle or all at once.
The programming challenges are graded right here in your browser: your program runs on several test inputs and its output must match exactly. At the Systems and Research levels, measure instructions, cycles and CPI, compare single-cycle, multi-cycle and pipelined designs, and see how a cache changes memory behaviour.
Program (Bench-8 assembly)
Assembles: 13 bytes. Loaded.
Instruction set (16 instructions)
| op | code | does | cycles |
|---|---|---|---|
| NOP | 00 | —. Do nothing for one instruction. | 5 |
| LDI n | 10 | A ← n. Load the number n itself into A. | 6 |
| LDA a | 20 | A ← M[a]. Load the byte stored at address a into A. | 7 |
| STA a | 30 | M[a] ← A. Store A into memory at address a. | 7 |
| ADD a | 40 | A ← A + M[a]. Add the byte at a to A (8-bit wraparound, C = carry out). | 8 |
| SUB a | 50 | A ← A − M[a]. Subtract the byte at a from A (C = 1 means a borrow: A was smaller). | 8 |
| AND a | 60 | A ← A AND M[a]. Bitwise AND with the byte at a. | 8 |
| OR a | 70 | A ← A OR M[a]. Bitwise OR with the byte at a. | 8 |
| XOR a | 80 | A ← A XOR M[a]. Bitwise XOR with the byte at a. | 8 |
| NOT | 90 | A ← NOT A. Flip every bit of A. | 6 |
| JMP a | A0 | PC ← a. Jump: the next instruction comes from address a. | 6 |
| JZ a | B0 | if Z: PC ← a. Jump to a if the last result was zero. | 6 |
| JC a | C0 | if C: PC ← a. Jump to a if the last ADD carried (or the last SUB borrowed). | 6 |
| IN | D0 | A ← input. Read the next number from the input tape (0 if the tape is empty). | 6 |
| OUT | E0 | output ← A. Write A to the output display. | 6 |
| HALT | F0 | stop. Stop the clock. | 6 |
- no signals yet: press Step
Next: F1 MAR ← PC.
Machine state as text
Cycle 0, 0 instructions completed. Next step: F1 (fetch) MAR ← PC. PC 0, IR 0 (NOP), operand 0, MAR 0. A 0, B 0. Flags Z 0 C 0 N 0 V 0. Output: nothing yet.
Output
–
Instructions
0
Cycles
0
CPI
–
Listing and breakpoints
| bp | addr | bytes | source |
|---|---|---|---|
| 00 | 10 05 | ▶ LDI 5 | |
| 02 | B0 0A | loop: JZ done ; stop when A is 0 | |
| 04 | E0 00 | OUT | |
| 06 | 50 0C | SUB one | |
| 08 | A0 02 | JMP loop | |
| 0A | F0 00 | done: HALT | |
| 0C | 01 | one: DB 1 |
Memory (256 bytes)
| + | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 00 | 10 (PC) | 05 | B0 | 0A | E0 | 00 | 50 | 0C | A0 | 02 | F0 | 00 | 01 | 00 | 00 | 00 |
| 10 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 20 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 30 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 40 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 50 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 60 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 70 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 80 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 90 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| A0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| B0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| C0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| D0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| E0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| F0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
blue = PC, lime = MAR. Program and data share this memory.
Cycle-by-cycle trace
| cycle | step | phase | transfer | bus | A | PC |
|---|
Nothing yet. Step the clock.
Saved programs stay in this browser only (localStorage). Nothing is uploaded.
Programming challenges
Each challenge runs your program on several input tapes, right here in your browser. Every output must match exactly, and the program must HALT.
Add two numbers Level 1+
Read two numbers with IN, output their sum with OUT, then HALT. Sums wrap at 256, just like the register.
Hint
ADD only adds a value from memory, so store the first number somewhere (STA) before you read the second.
Performance (Level 4+)
Run a program (Run to end) to measure it.
Single-cycle vs multi-cycle vs pipeline
Run a program first: the comparison replays the exact instructions it executed.
Experiment: throughput vs hazard rate
The same model with a synthetic instruction mix, as a Simulation OS experiment: change one variable at a time and compare runs.
Pipeline throughput vs hazard rate
How data hazards and taken branches eat into the speed-up of a 5-stage pipeline, with and without forwarding.
- 1. Ask
- 2. Hypothesise
- 3. Set up
- 4. Run
- 5. Record
- 6. Change one variable
- 7. Compare
- 8. Interpret
- 9. Limitations & save
If I change …, then … will …, because …
3 · Set up the parameters
Longer runs hide the 4-clock fill time of the pipeline.
Fraction of instructions that need the result of the instruction just before them.
Forwarding passes a result straight from EX or MEM to the next instruction.
A value read from memory is ready one stage later, so even forwarding stalls once.
Fraction of instructions that are jumps.
2 more controls appear at higher levels; until then they stay at the default value.
The seed fixes any random noise. Same parameters + same seed = exactly the same result, every time, on every computer.
6 · Change one variable
Your first run is the baseline. After it, change one thing at a time.
7 · Compare on a graph
5 · Run log
No runs yet.
Equations
stalls = (forwarding ? H_load : 2·H) + B_taken · penalty, with H = round(N·h), H_load = round(H·L), B_taken = round(N·b·t). Speed-up = 5N / cycles, since a single-cycle clock must fit all 5 stages.
Assumptions (always true in this model)
- Every stage takes exactly one time unit; there is no pipeline-register overhead.
- Every hazard is between neighbouring instructions (distance 1).
- Branches are predicted not-taken and resolved in EX.
- No structural hazards, cache misses or interrupts.
- Counts are rounded to whole instructions, so results are whole clocks.
Text description of what the chart shows
A line chart of clocks per instruction (CPI) against hazard rate from 0 to 1. Without forwarding CPI climbs steeply, by about 2 for every hazard per instruction; with forwarding it stays close to 1, rising only for hazards that follow a load. Taken branches lift both lines by the branch penalty.
Did the result support your hypothesis? Use the numbers from the comparison table and link them to an equation.
Which assumptions could change your answer in the real world?
Export and save
Exports use the ticked runs (or every run if none are ticked). Your draft saves automatically in this browser only. The Journal is private to you.
The cache simulator opens at Level 5 (Research Engineer).
Check yourself
Marked instantly in your browser. Use the interactive above to work it out.