Elements of Computing Systems (nand2tetris), Chapter 1

This thread is for discussing the first chapter of The Elements of Computing Systems and the relevant videos from the Coursera course. This top post is wiki-style, so anyone can update it with a summary based on the discussion below.

  • wiki summary goes here.

I’m curious to see how other people are solving the exercises in the book. My current workflow is to write out some pseudocode on paper and pass zeros and ones through it, adjusting things until it works. I’m not sure that I like my solutions. I like @Dan’s illustrations on Github, but for some reason my brain has trouble solving the exercises that way. Maybe I’m missing a step or some way of thinking about it.

Example workflow:

Click to show Xor gate (contains code for book exercises)

I start tinkering with something like this:

Xor = Not(And(Nand(Not(a), b), Nand(a, Not(b))))

Then translate it to the HDL:

CHIP Xor {
    IN a, b;
    OUT out;

    PARTS:
    Not(in=a, out=nota);
    Not(in=b, out=notb);

    Nand(a=nota, b=b, out=c);
    Nand(a=a, b=notb, out=d);

    And(a=c, b=d, out=e);
    Not(in=e, out=out);
}

I’m wondering if there is a good way to simplify the expressions or find a better workflow for solving them. Another solution for Xor that I looked at uses only three gates, so I can see that my answer is twice as long as it could be.

A 3-gate solution (contains code for book exercises)

I saw this online:

CHIP Xor {
    IN a, b;
    OUT out;

    PARTS:
    Or(a=a, b=b, out=c1);
    Nand(a=a, b=b, out=c2);
    And(a=c1, b=c2, out=out);
}

There may not be a simple answer to my question, and it could be that I just need to practice building more gates, but I would like to be able to figure out how to get my brain to think about the problems more clearly, possibly starting from illustrations. :slight_smile:

In most of my hdl files, I included some comments on my thinking for that particular chip. Looking back through those, I think two things helped me to work through these.

One was to say in words what a gate was supposed to do. For example,

Click for Xor explanation.

The exclusive or, a Xor b, means "a or b, but not both a and b."
Thus, a Xor b = (a or b) and not(a and b).

And the second helpful thing, which probably played the larger role here, was that years ago in college I took a class that included a section on symbolic logic. Amazingly (to me), the text for this course is now online. The first few sections of the chapter on logic may be helpful to some folks.

1 Like

Thanks, that example is helpful. I’ll start reading the section on logic in that book.

1 Like

You may have noticed this already, but there’s also a pdf version of the whole text.

I really loved that class. I think it played a big part in my choosing to major in math.

1 Like

I wonder if there are any related lectures online.

I don’t think so, but I haven’t looked. The original book was written by the prof who taught it to me, finishing it during the summer right before I took the course. It was definitely still in beta then.
Evidently it’s since been changed a bit by the second author (whom I took Pascal from ages ago, haha). So far as I know, the book has only been used in house at that school.