[ Back to Blogs ]

Nand2Tetris

2025-05-03


I first tried to start Nand2Tetris with the course videos and found them hard to understand. Then around 2 months later I found out that the Nand2Tetris guys have a book as well, called Elements of Computing Systems, and I prefer text-based tutorials/guides more than video-based ones. Not to my surprise, it had better explanations and was easier to understand, even though it's written by the same professors who made those video lectures, Noam Nisan and Shimon Schocken. I feel like for something that is mainly text-based like programming, it's easier to process information through books rather than YouTube courses/tutorials. So this is me sharing my journey through this wonderful course.

Projects 1 to 5 were mostly easy to implement. I had seen the implementation of the same components in the book Code by Charles Petzold not so long ago and implemented them myself in the Turing Complete video game, which is also similar to this course where you build a Turing-complete computer from basic logic gates and work your way up.

Except for Project 4, the one where we had to write assembly. But it felt easy once I realized that the A register is like a pointer in C, M is like a dereferenced pointer, and the D register is a general-purpose register you can use to move values around. For example:

We want to do J = 5:

@5
D=A
@J
M=D

Here we store the value of A (which is 5) in the D register, then use that register to store 5 into the memory address of the variable J.

But now let's say we want to do X = J. Since J is a variable and not a number, A will hold the address of J (like a pointer in C), and M will be the value stored at that address. So D=A would be wrong here. The correct version is:

@J
D=M
@X
M=D

Just understanding this one concept made Project 4 easy.

Project 6 I enjoyed a lot. Since I mostly worked with strings in C, I knew how to handle them, and this project was all about reading Hack assembly and writing the binary equivalent to another file. It didn't require much thought. I had a little trouble with the symbol table since I thought it required dynamic arrays, and C doesn't have those. I thought I'd have to do it the Tsoding way, but then I realized the Hack computer only contains variables from RAM[16] to RAM[255], so it's a fixed maximum number of variables that can't grow dynamically like the stack.

Projects 7 and 8 are the VM translator. The first part with just the arithmetic instructions wasn't that hard. Let's walk through the SimpleAdd test from Project 7.

push constant 7 tells us to store 7 somewhere and push it onto the stack:

@7
D=A
@SP
A=M
M=D
@SP
M=M+1

@SP is the stack pointer that points to RAM[0]. The assembler maps @SP to RAM[0], and RAM[0] contains a pointer to RAM[256+]. So M of @SP starts at 256 and grows upward, it's like a **ptr in C. By doing A=M, A now equals 256, pointing at RAM[256], and we store D there with M=D. The stack pointer is supposed to point to the next available free location just above the most recently pushed item, so we finish with M=M+1. Now the stack pointer points to RAM[257]. We do the same for push constant 8.

Now how do we add them?

@SP
M=M-1
A=M
D=M
A=A-1
D=D+M
M=D

After pushing two values, 7 is in RAM[256] and 8 is in RAM[257]. The stack pointer is currently at RAM[258]. M=M-1 decreases it to 257. A=M makes A point to RAM[257], so M contains 8. D=M stores 8 in D. A=A-1 makes A point to RAM[256], so M contains 7. D=D+M gives us D = 8+7 = 15. And no, it's not 8 = 8+7 -- as we learned in Project 4, the left side of = is the destination and the right side is the computation. Finally M=D stores 15 into RAM[256].

After understanding this, figuring out push local 0, push arg, etc. wasn't hard.

Project 8 is a work in progress. For 2 days now I haven't been able to understand how call, function, and return are supposed to tie together across multiple files in a directory, and what the Sys.init function is about, even though I was able to write the ASM using the instructions given in the book. I'll update this blog when I'm done with it.

I'll keep updating this as I go through the course.

Source code for my implementation is at github.com/Eclipse1745/Nand2Tetris.

eclipse  ยท  03-05-2026