Tuesday, February 13, 2018

NAND to Tetris: Nothing short of awesome

Been a while since I posted, huh?

Well, here's an interesting development in my life: I finally started the NAND to Tetris course (part 2) on Coursera, which is based on the instructors' site.

I can say, whole-heartedly, that the course is awesome, and the first thing in a long time that has gotten me into a flow state.

The idea behind the course is this: they take you through (virtually) building a computer from a very basic level, showing all the abstraction layers that produce a computer's behavior.

It starts with logic gates -- specifically, the NAND gate (hence the name), since it's universal. Your first project is to use a hardware simulator to build several other logical gates from NAND. (NAND is just an AND gate with the outputs inverted, so that true and true yield false, everything else yields true.) The second project is to to build the arithmetic logic unit (ALU) in a CPU out of the components, so you basically have a configurable circuit: based on six control bits, you perform one of several possible functions on two 16-bit inputs.

The next project incorporates "flip flops", which allow you to repeatedly execute that circuit while also writing to and reading from some some persisted memory. Your inputs to the circuit then function as a kind of machine code.

The project after that then has you implement functions in that machine code, writing in an assembly language the authors created for the course that has a precise mapping to the machine code inputs in the CPU from the previous lesson. I can honestly say it was a really fun project to implement multiplication directly in assembly language!

Later on you write a compiler from a high level language into assembly (which then converts simply into machine code), in a way that's broken into two steps: a compiler from the high level language into a virtual machine that works on a stack with memory blocks, and a compiler from the virtual machine commands to assembly. I just recently finished that latter part (which comes first).

Those layers then build up to making a game that runs on your CPU, then an OS, and some other stuff I haven't delved into.

In the course of the projects, you use a hardware simulator, an assembler (for converting the assembly language into machine code), a CPU simulator, and a virtual machine simulator.

In order to catch up with the class an have enough leeway for a weekend trip, I went through much of the course over the weekend, and enjoyed every minute of it! I especially like how the break the projects into manageable pieces. For example, in the VM-to-assembly part, it first has you implement simple push/pop/add operations on a stack and run a test to verify that you can compile those commands. Further test suites give you a manageable set of operations to add.

I've written an actual compiler now! (albeit limited use...)

(Consider how fun this is, and how naturally it comes to me, maybe I picked the wrong field.)

One interesting challenge is that the CPU only has two registers call A and D, and only the A register is used when accessing memory, to know which word of memory to look at. It took me a while to figure out how you could say "look at the value in the memory location refered to in the memory location zero." Before I saw how you could do it, I implemented one project by having two separate code branches for the two possible values at the memory location!

Would love to link my code for the projects, but they discourage that to leave the challenge for others to solve.

10 comments:

sheela rajesh said...
This comment has been removed by a blog administrator.
seoexpert said...

A debt of gratitude is in order for the significant data and experiences you have so given here...play tetris unblocked online

bamgosoo said...


Will really appreciate the writer's choice for choosing this excellent article appropriate to my matter.Here is deep description about the article matter which helped me more



Visit my site :: 안마
(jk)

jenny said...


Really informative and inoperative, Thanks for the post and effort! Please keep sharing more such blog if want
more information about topic Visit Us our Website at movies flame .

crypto said...

Cryptoanime is one of the best website that post amazing anime reviews. Their reccomendations are amazing.

can you take ged classes online said...

This journey seems both intellectually stimulating and creatively fulfilling. It's wonderful to see the joy and passion you've found in this course, and your inclination and natural grasp of the subject matter hints at a promising path in the field of computer science.


Elizabeth Johanson said...

Once, a friend recommended this course to me, but right now, I'm having trouble with OTHM and am looking for help with partnership working in health and social care. I need professional help. I'm worried.

williamsdavid said...

The project after that then has you implement functions in that machine code, writing in an assembly language the authors created for the course that has a precise mapping to the machine code inputs in the CPU from the previous lesson. Last year i shipping my PC to my fried through Ocean Freight Logistics Englewood in USA

keven john said...

This is a good place, When I went to Australia,I visited this place,Now Iam study at university and busy at my work that is cheap dissertation writing ervice Uk,After the study once again,I have visited this place.

Daxton Beckett said...

Amidst the tornado of NAND gates and logic circuits, I could not stop thinking about my Custom Law Thesis being stored in the forgotten corner of my hard drive. Both, in their own way, required perfection through infinitesimal detail, careful examination, and tireless effort towards the optimum outcome. The NAND, the basic of all digital felt like the elementary legal principles that I unpacked in my thesis. Each line of code, every transistor connection, reflected the tangled debates I wove, one by one, like logical bricks.