Machine Language
Machine language: 16 bits, nothing else
Machine language is binary code: 16 bits per instruction, one instruction per cycle.The human-readable form is only mnemonics and symbols; the real machine language is these 0s and 1s.
01What machine language is
Machine language is the only thing a CPU executes directly: a stream of 16-bit binary code.It answers three questions: which operations exist, what they operate on, and where the next instruction is.
| Question | The answer in machine language |
|---|---|
| Which operations | Compute (18: constants, copy, negate, add/subtract, and/or), read memory, write memory, jump |
| What they operate on | Only three places: register A, register D, and M (= RAM[A], address first put into A) |
| How flow is controlled | PC = PC + 1 by default; to jump, A first holds the target, then the jump field decides; conditions come from the ALU’s zr / ng |
Remember those three and every other instruction is a combination: multiplication is “loop + add + test”, drawing is “compute an address + write M”. Machine language has no notion of multiply, functions or arrays.
The human-readable form (@5, D=M+1) is symbolic syntax: the names inside are mnemonics and symbols. The real machine language is only 0 and 1.
02What an instruction looks like: only two kinds
The top bit tells them apart: 0 starts an A instruction, 111 starts a C instruction. There is no third kind.
| Instruction | binary syntax | What it does |
|---|---|---|
| A | 0vvvvvvvvvvvvvvv | puts a 15-bit number into A |
| C | 111accccccdddjjj | computes comp, stores into dest, jumps by jump |
| (LABEL) | — | not an instruction: a name for an address, absent from the binary |
03The A instruction: symbolic and binary
symbolic is @value (a number or a symbol); binary is 0 plus a 15-bit value.
| symbolic | binary | What it does |
|---|---|---|
| @5 | 0000000000000101 | A = 5 |
| @32767 | 0111111111111111 | A = 32767 (the largest 15-bit value) |
| @SCREEN | 0100000000000000 | A = 16384 (address of the first screen word) |
| @sum | 0000000000010000 | A = 16 (the RAM address of variable sum) |
An A instruction does one thing: write A. It does not read memory, touch D, or jump. It exists because the CPU has a single address line and it is wired to A — so the address must go into A before M can be used.
04The C instruction: three fields
symbolic is dest=comp;jump; binary is 111 plus 13 bits, split into four fields:
| Field | Bits | Controls |
|---|---|---|
| a | 1 | whether comp uses M: 0 means “use A instead of M” |
| comp | 6 | which of the 18 operations to compute |
| dest | 3 | where to store: any combination of A / D / M (or nothing) |
| jump | 3 | whether to jump: 7 conditions (or not at all) |
Within one instruction the order is fixed: compute, then store, then jump. Storing and jumping use the same comp result.
05Line by line: symbolic against binary
Each line gives the symbolic form, the binary form (split as 111 | a | comp | dest | jump), what operation it is, what it operates on, and whether it changes the flow.
| symbolic | binary (split) | Operation | Operates on | Changes flow? |
|---|---|---|---|---|
| D=A | 111 0 110000 010 000 | copy | puts A into D | no |
| D=M+1 | 111 1 110111 010 000 | add | adds 1 to RAM[A], stores into D | no |
| M=D | 111 0 000110 001 000 | store | writes D into RAM[A] | no |
| D=D&A | 111 0 000000 010 000 | bitwise AND | D AND A, back into D | no |
| D;JEQ | 111 0 000110 000 010 | conditional jump | if D = 0, PC = A | yes |
| 0;JMP | 111 0 101010 000 111 | unconditional jump | PC = A | yes |
In the last two rows dest is empty: they only test, writing nothing back. So a C instruction does not have to store anything.
06Control flow: two lines working together
The CPU by itself only does PC = PC + 1. To jump it takes two things: supply the address (A instruction), then decide whether to jump (the jump field).
@sum // A = the address of sum (A instruction) D=M // D = RAM[sum] (C instruction) @END // A = the address of END (A instruction) D;JEQ // if D == 0 → PC = A (C instruction, jump)
Where do conditions come from? The ALU’s zr / ng: zr when the result is 0, ng when it is negative. The three jump bits test those two flags:
| jump | Condition | Jumps when |
|---|---|---|
| JGT | 001 | result > 0 |
| JEQ | 010 | result = 0 |
| JGE | 011 | result ≥ 0 |
| JLT | 100 | result < 0 |
| JNE | 101 | result ≠ 0 |
| JLE | 110 | result ≤ 0 |
| JMP | 111 | always |
So a loop in machine language is: a line @LOOP puts the address back into A, and the next line 0;JMP jumps there unconditionally.
07Build a C instruction
Pick dest, comp and jump, and watch the 16 bits take shape.
11100111110100000xE7D0D=D+1