CS ATLAS A map of computing

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.

QuestionThe answer in machine language
Which operationsCompute (18: constants, copy, negate, add/subtract, and/or), read memory, write memory, jump
What they operate onOnly three places: register A, register D, and M (= RAM[A], address first put into A)
How flow is controlledPC = 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.

Instructionbinary syntaxWhat it does
A0vvvvvvvvvvvvvvvputs a 15-bit number into A
C111accccccdddjjjcomputes 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.

01501401301201101009080706051403120110opcode 0value (15 bits)
symbolicbinaryWhat it does
@50000000000000101A = 5
@327670111111111111111A = 32767 (the largest 15-bit value)
@SCREEN0100000000000000A = 16384 (address of the first screen word)
@sum0000000000010000A = 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:

11511411301201111019181716051403020100111acompdestjump
FieldBitsControls
a1whether comp uses M: 0 means “use A instead of M”
comp6which of the 18 operations to compute
dest3where to store: any combination of A / D / M (or nothing)
jump3whether 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.

symbolicbinary (split)OperationOperates onChanges flow?
D=A111 0 110000 010 000copyputs A into Dno
D=M+1111 1 110111 010 000addadds 1 to RAM[A], stores into Dno
M=D111 0 000110 001 000storewrites D into RAM[A]no
D=D&A111 0 000000 010 000bitwise ANDD AND A, back into Dno
D;JEQ111 0 000110 000 010conditional jumpif D = 0, PC = Ayes
0;JMP111 0 101010 000 111unconditional jumpPC = Ayes

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:

jumpConditionJumps when
JGT001result > 0
JEQ010result = 0
JGE011result ≥ 0
JLT100result < 0
JNE101result ≠ 0
JLE110result ≤ 0
JMP111always

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.

dest
comp
jump
11100111110100000xE7D0

D=D+1