GitHunt
PZ

pzuehlke/NAND2Tetris

Implementations of all the projects in the NAND2Tetris course/book

Nand to Tetris

This repository contains my solutions to the projects in the Nand to Tetris course (parts I and II). The files are organized according to the projects to which they belong. No built-in code, test scripts, or auxiliary files are included, because these can already be found on the course's website and may change in the future. The only exceptions are contained in the .zip files (one per project) which are my submissions to the Coursera grader and sometimes consist not of the source code needed to solve the projects, but of the result that they produce when run on some given test files.

Part I

  • Project 1: Build several single- and multi-bit elementary logic gates (such as NOT, AND, OR, XOR, multiplexers and demultiplexers) using the NAND gate as the only given primitive.
  • Project 2: Implement a half-adder, a full-adder, a (16-bit) adder, a (16-bit) incrementer and a simple ALU (arithmetic logic unit).
  • Project 3: Build chips that can store data using only DFFs (data flip-flops) as primitives gates. These include 1- and 16-bit registers, RAMs of several sizes (containing from 8 to 16K registers) and a program counter to be included in the CPU.
  • Project 4: Write two simple programs in symbolic assembly language.
  • Project 5: Build a "computer" by putting together the CPU, a 16K RAM, memory maps for screen output and keyboard input and a ROM. The latter three are provided as primitives, but differ little from the RAMs built in Project 3.
  • Project 6: Implement an assembler. I chose to do this in Java. The assembler automatically translates symbolic assembly language into binary code that the hardware built in Project 5 can understand and execute.

Part II

  • Project7 and Project 8: Implement a VM translator that converts VM code based on a stack into Hack assembly code. This was done using Java. My original solution to Project 8 passed all tests except the one for "FibonacciElement" due to a bug which took me a lot of time to find. It is easy to make a mistake in the assembly code when manipulating pointers.
  • Project 9: ...
  • Project 10: Implement a syntax analyzer for the Jack language, consisting of a tokenizer and a parser. For testing purposes it translates the source code into an XML document which model the tree-like structure of the code. This project requires a lot of grinding.
  • Project 11: Implement the code generation part of the compiler, which uses the services of the syntax analyzer to translate Jack code to VM code.
  • Project 12: Implement the OS through eight modules written in Jack, including memory management, input and output handling, and system bootstrap. The specifications for the memory module in the video lectures and in the book (2nd ed.) are inconsistent. My solution follows the description in the book.

Official resources

  • The official website of Nand to Tetris, and in particular the online IDE, which is all that one needs to complete almost every project. I couldn't find the target .xml files for Project 10 inside the IDE, had to download them from this link. Also, as of the time of writing, the IDE doesn't seem to have the "TextComparer" tool implemented, so I just used diff -w.
  • The accompanying book: Nisan, N. and Schocken, S. — Elements of Computing Systems (2nd ed., MIT Press).
  • The Coursera course(s): Part I and Part II, containing the video lectures. These lectures follow the book pretty closely, with some extra comments sprinkled in and some tutorials on the utilities provided in the IDE. Like every other resource associated with the course, they are very well made. However, it is not necessary to watch them in order to complete the projects. I preferred to use the book when actually doing the projects since it is much easier to find the various specifications there than it is to search for the right places in the videos.
  • A TED Talk by Prof. Schocken about the Nand to Tetris course and an app co-developed by him called Matific, which teaches Math topics to children using the same learning-by-doing philosophy.
  • An interesting podcast episode where he explains what motivated him to leave a tenured position at NYU to go back to Israel and help found Reichman University, how Nand to Tetris was planned and created, his views on learning and teaching and some other topics.

Errata

Here is a list of typos that I found while reading the book (2nd ed.), none of them significant:

  • Page 39, 2nd line after the table: "if (zx==1)" should be "if (zx=1)" for consistency.
  • Page 67, last line: ";0" should be "0;".
  • Page 146, last paragraph: "the Microsoft's .NET" should be "Microsoft's .NET".
  • Page 150, 2nd paragraph of Example: ng should be neg.
  • Page 161, 4th line after Figure 8.5: "affects" should be "effects".
  • Page 174, 2nd paragraph, 3rd line: "Application Program Interface" should be "Application Programming Interface".
  • Page 213, Figure 11: In the field declarations of the Point.jack program, ther is a missing semicolon at the end.
  • Page 246, 4th line: "we better" should be "we'd better".
  • Page 251, 1st and 2nd lines of Figure 12.3: missing spaces in "part ofy" and in "integery".
  • Page 261, 3rd line in the paragraph titled "Init functions": "fucntion" should be "function".

Some personal comments

This course and the associated resources are a pedagogical masterpiece. One can only imagine the amount of work it must have taken to produce all of the course
material. More than any other MOOC I have taken, Nand to Tetris manages to transmit an incredible amount of knowledge and intuition per time/space. If you are at all interested in computers and have the necessary time, I strongly recommend you give it a try. However, for Part II I you should be comfortable with some object-oriented programming. (The Coursera auto-grader currently only accepts Python or Java code, but the NAND2Tetris course itself is language-agnostic.) In Part I, only project 6 requires programming experience.

Languages

Assembly63.7%Java36.3%

Contributors

GNU General Public License v3.0
Created October 9, 2024
Updated September 7, 2025