2005 · Noam Nisan and Shimon Schocken
Hack (nand2tetris)
Hack is the computer at the heart of nand2tetris, the course that takes students from a single NAND gate to a working computer that runs Tetris-like games. It is small on purpose: two instruction types, three registers, and nothing you cannot hold in your head.
| Designed by | Noam Nisan and Shimon Schocken |
|---|---|
| Book | The Elements of Computing Systems (MIT Press, 2005; 2nd edition 2021) |
| Word size | 16 bits |
| Registers | A (address/data), D (data), PC; M is shorthand for RAM[A] |
| Instruction memory | ROM32K: 32K words, separate from data (Harvard design) |
| Data memory | 16K words of RAM, plus memory-mapped screen and keyboard |
| Screen | 512 × 256 black-and-white, mapped at address 16384 |
| Keyboard | One word at address 24576 |
| Instruction types | 2: the A-instruction and the C-instruction |
Why it was built
Noam Nisan and Shimon Schocken wanted students to understand a computer all the way down, not as a stack of black boxes. Their course, now known everywhere as nand2tetris, starts with one primitive gate and builds upward through logic, arithmetic, memory, a CPU, an assembler, a virtual machine, a compiler and an operating system. Hack is the hardware platform for that journey, designed so that every part of it can be built and understood by a student in a few weeks.
If you have not taken the course, we recommend it warmly. The book The Elements of Computing Systems and the free materials at nand2tetris.org are among the best introductions to computing ever written, and the projects are a joy.
How it works
Hack has a Harvard design: programs live in a read-only instruction memory, ROM32K, and data lives in a separate RAM. Every word is 16 bits. The CPU has just two programmer-visible registers. D holds data. A holds either data or an address, and whatever address is in A selects the memory word called M. The program counter decides which instruction comes next.
There are only two kinds of instruction, distinguished by the top bit:
| Instruction | Form | What it does |
|---|---|---|
| A-instruction | @value | Top bit 0; loads a 15-bit constant into A. Used for numbers, addresses and jump targets. |
| C-instruction | dest=comp;jump | Top bit 1; computes something with the ALU, optionally stores it in A, D and/or M, and optionally jumps. |
A typical step looks like @17 followed by D=D+M: point A at address 17, then add that memory word into D. A jump is two lines too: load the target into A, then compute something and test it, as in D;JGT (jump if D is greater than zero).
The ALU supports a small menu of operations on D and A or M: constants 0, 1 and −1, negation, bitwise NOT, AND and OR, increment, decrement, addition and subtraction. There is no multiply; that is left to software, which is part of the lesson. For the general fetch-decode-execute picture, see how a CPU works.
Memory-mapped I/O
Hack has no I/O instructions. The screen and keyboard are simply parts of memory. Starting at address 16384, 8,192 words hold the screen: 256 rows of 512 pixels, 16 pixels per word. Writing a 1 bit turns a pixel black. The word at address 24576 holds the code of the key currently pressed, or 0 if none. To draw, you store; to read the keyboard, you load. That is all there is to it, and it is how many real machines work.
Why it matters
The Hack platform has been used by universities, high schools and self-learners around the world, and the course has been taken by a very large number of people online. It shows that a complete, programmable computer does not need to be complicated, only well designed. For a lot of programmers, Hack is the first computer they ever built.
Hack in NANDSIM
The museum Hack is rebuilt from NAND gates and runs real Hack programs, with the screen and keyboard mapped where you would expect. Open it up to inspect A, D, the program counter and memory as programs run.