Inside a Computer: From Assembly to a Tiny Compiler

Program an 8-bit CPU one instruction at a time, then watch a compiler turn a few lines of code into tokens, a tree and machine instructions.
Every program you run ends up as simple instructions: load a number, add, compare, jump. Two new labs let you see that whole journey, from the bottom up.
A computer you can read in full
The Retro CPU emulator runs PT-8, a small 8-bit computer of our own design. It has 256 bytes of memory, two registers (A and B), a program counter, a stack pointer and three flags:
- Z is set when a result is zero
- C is set when a sum carries past 255 or a subtraction borrows
- N is set when the top bit of a result is 1
Load the Hello, world example and press Step. Each press runs one instruction: you can watch B point at the next letter, A load it, and OUTC print it, until a zero byte ends the loop.
Why the carry flag matters
Load the Fibonacci example. It adds numbers until one no longer fits in a byte: 144 + 233 is 377, more than 255, so the carry flag goes on and JC jumps out of the loop. Old home computers handled bigger numbers exactly this way, one byte at a time, passing the carry along.
Programs and data share the same 256 bytes. A program can even change its own instructions, which is how many old machines saved space.
What a compiler actually does
The Compiler explorer takes a small language with variables, arithmetic, if and while, and shows four stages side by side:
- Tokens: the scanner cuts the text into numbers, names and symbols.
- Syntax tree: the parser works out what belongs to what. In 2 + 3 * 4 the multiplication sits lower in the tree, so it is done first.
- Stack machine code: PUSH, LOAD, ADD, JZ and friends, ready to run.
- Output: the stack machine runs the code.
Untick Fold constants and compare: with folding on, a line such as x = 60 60 24 becomes a single PUSH 86400 before the program ever runs.
A detour into shapes
If you enjoy seeing how simple rules build complex things, the Topology lab does it for surfaces. Glue the edges of a square as a b a' b' and you get a torus; change one arrow to a b a b' and you get a Klein bottle, a surface with only one side.
Try it
Write a loop in PT-8 that counts up instead of down, then write the same loop in the Compiler explorer and compare the instructions each one needs.