Constructive Critiques of NAND2Tetris
2026-11-27
I had the opportunity to teach our Computer Organization course for the first time this semester, something that has somehow always fallen on other people despite my decade at Occidental. One reason I was excited to teach the course is that we use The Elements of Computing Systems - also known as NAND2Tetris - as our syllabus, which I have never worked through. Funnily enough, I have played through Turing Complete, and so I unsurprisingly had a lot of fun working through the first half of the projects (up to building a functional CPU).
Since we are/did not following the second half of the book - our goals for the course meant students built something akin to x86 assembly and C, as opposed to a stack-based VM and an OOP-based Java-like - I wanted to write down a few annoyances I had with a number of design decisions from the book. I wrote my own simulator for the course, foregoing the online IDE, which made it possible to address some of the issues on the fly. At the same time, since I was only working two weeks ahead of students, I failed to foresee the downstream effects of some of the technical choices.
-
I have a number of grievances about the hardware description language (HDL), the major ones of which are all exemplified by the
ngoutput of the ALU. For context, at this point, the ALU has calculated a 16-bit result in two's complement and sent it to theoutoutput bus. Thengoutput should be set to1ifoutis negative, that is, if bit 15 (the most significant bit) ofoutis a1. My initial approach was to take part ofoutwith:Mux16(..., out=out); And(a=false, b=out[15], out=ng);But this resulted in an error on the IDE, because output buses are not allowed to be inputs to another chip. I then thought I could use a temporary bus instead:
Mux16(..., out=temp_out); And(a=false, b=temp_out[15], out=ng);Which led to a different error: since
temp_outis an internal bus, I am not allowed to subscript it either. My final attempt was to mask the entire value and maybe extract that first bit some other way:And(a=0x8000, b=temp_out, out=temp_ng);But of course, it turned out that HDL doesn't let you specify constants either, outside of
true(either0b1or0xFFFFdepending on context) orfalse(0b0or0x0000). At this point, I gave up completely and just Googled for the solution. The expected and only solution turned out to be:Mux16(..., out=out, out[15]=ng);The
out[15]=syntax is completely undiscoverable: there is no indication up to this point that a chip can have outputs listed twice, never mind that we can only take parts of it with the square bracket syntax. That might be excusable, except that the syntax systematically disallows any other solution, for seemingly no reason. Why can't I branch from an output bus as input to another chip? Why can't I subscript internal buses but can subscript external ones? Why can't I specify arbitrary binary constants? Maybe real HDLs are equally contrived, but that's not a reason to carry it into an educational setting.In creating my own HDL, I made a number of changes to remove this friction:
-
I allow specifying constants in binary or hexadecimal
-
I allow internal buses to be subscripted
-
I allow output buses to serve as inputs to chips
-
I didn't do it this semester, but in the future I will require students write
CopyandCopy16chips as part of the first project
-
-
I made a few more unrelated changes to the HDL:
-
I allow HDL files to have multiple
CHIPdefinitions. This was useful for creating utility chips; two from me and from students includeXor16andFrom1To16(which copied a single bit to 16 bits). -
I'm thinking about requiring each chip to list all internal buses and their widths with an
INTERNdeclaration, in the same way that each chip hasINandOUTdeclarations. This simplifies the underlying error checking, and also forces students to think about how wide each bus is. -
In the future, I will replace the subscript slicing syntax from
bus[0..2]tobus[2:0]. This more closely matches Python, which students would have seen in the prerequisite course, and also matches the positional notation for which bit is index 2 and which bit is index 0. -
Entirely cosmetically, I allow trailing commas, which makes version control diffs nicer.
-
-
As far as I can tell, the
Or8Waychip is only used for the ALU, to determine if the output is 0. If that is it's only intended use, it's defined in a deliberately awkward way, that requires separately dealing with the two halves of an output before combining them. In the future, I will replace it with aZerochip that directly determines if a 16-bit value is 0. -
Clocks and sequential chips are not explained well and cause a lot of confusion. I understand the need to include them, but the level at which is it explained is both too deep and too shallow.
-
It is too deep because students don't really have to think about the clock when doing the projects. For the purpose of combining gates into circuits, a (gated) SR AND-OR latch is much easier to understand, since students could trace through the circuit and convince themselves of the behavior. With the data flip-flop
DFFchip - which students don't implement - there were a lot of questions about how it works, and the material about the rising/falling edges only add to the confusion. Making this worse is that these concepts are never brought up again; especially given that the alternative of SR latches exist, the discussion felt like an unnecessary detour. -
At the same time, this discussion of clocks is not deep enough to clarify a point of confusion in the later chapter on CPUs and overall computer architecture. The CPU chip outputs both an updated value of memory, as well as the next memory address to read - but it's the value of the old address that changes, and that old address is not output anywhere. If there is one place where explaining the clock matters, this would be it; instead, this is handwaved as the new value being instantaneous while the address is clocked. Lots of other people are confused by this too, so it's a clear area of improvement. For my class, I ended up requiring the CPU to output both old/write and new/read addresses; if I had known about this earlier, I would also have required the RAM chips to take two addresses as inputs.
-
-
The program counter
PCchip has a weird interface. I understand why thereset,load, andincinputs have the precedence they do, but conceptually it feels neater to have a chip that takes aninand aloadlike the other memory chips, except that if there is no load, the value increments itself. Whether to load a zero value is instead relegated to some other chip, which also fits the mental model of when reset should occur (ie. when the user presses a physical button on the computer). -
I dislike the separation of instruction and data memory, and have a minor nitpick about the memory mapping of the screen and the keyboard. I think having a single memory that stores both instructions and data - and in particular, allows arbitrary data to be (inadvertently) executed as instructions - is meaningful for students to experience. This has obviously downstream applications (eg. explaining buffer overflow attacks), and I don't think it requires many other changes to have a unified 15-bit addressable memory. If the total amount of memory is an issue, my nitpick is that the current memory maps leaves unusable addressable memory anyway, so I would move the screen to
0x0600-0x7FFF, put the keyboard at0x05FF, and have everything else be normal RAM. The approach of the book is to not take the 16-bit constraint seriously, which is also the approach I took; that said, if we want to take it seriously, we could just make everything 32-bits. -
I take issue with what the book calls "assembly". It's not wrong in that it's an instruction set that is isomorphic to machine code, but it was also too simplified and constrained. Instead, I told students that it's a form of microcode, on top of which we will be building "real" (read: x86-like) assembly. That said, I did find the design of the microcode elegant, in how the @ and C instructions are separated, in the notation for the C instruction, and in how well the C instruction machine code lines up with the ALU inputs.
-
I found the
CPUchip to be defined awkwardly. The test script checks the contents of the D register but not the A register, and the need to use theDRegisterchip also felt awkward. In the future, I would define aCPULogicchip that shrinks its boundaries exclude the registers themselves, but have output pins that correspond to the input pins of the registers. This makes testing more transparent, and the original CPU chip can simply connect this with the registers themselves. As an added bonus, sinceCPULogicdoes not contain any memory, it becomes a stateless combinatorial chip. -
Why is the destination component in a microcode instruction ordered "AMD", but the bits are ordered "ADM" in the machine code?
-
I don't understand why the book introduces the idea of "variables" in microcode, and go to the trouble of specifying that they are assigned sequentially to address
0x0010,0x0011, etc. This complicates parsing later, and the same benefit (namely, program execution depending on a value in memory) could be had just by using the "registers"R1...R15. -
The book has the convention of an infinite loop to indicate that a program is done. This is not very elegant; the only justification I could think of for this design is that, since the
Computerchip has no way of turning off, this allows "stopping" to stay in-simulation. I took a different approach: I defined aHLTsymbol, equivalent to the last possible instruction address at0x7FFF. Jumping to this address signals that a program is done, and moreover, theComputerchip now has ahaltoutput that should be0b1if that's the case.
I hope this is helpful for people teaching from NAND2Tetris in the future. I'm teaching the course again next semester, and one of my goals for winter break is to update my simulator to address everything in this list. I would also be happy to report on my experience adapting the book to x86 assembly and C at that time!