0

I'm working through this book http://nand2tetris.org/book.php that teaches fundamental concepts of CS and I got stuck where I'm asked to code an AND chip and test it in provided testing software.

This is what I've got so far:

 /**
 * And gate: 
 * out = 1 if (a == 1 and b == 1)
 *       0 otherwise
 */

CHIP And {
    IN a, b;
    OUT out;

    PARTS:
    // Put your code here:
    Not(in=a, out=nota);
    Not(in=b, out=notb);
    And(a=a, b=b, out=out);
    Or(a=nota, b=b, out=nota);
    Or(a=a, b=notb, out=notb);

}

Problem is I'm getting this error:

...
at Hack.Gates.CompositeGateClass.readParts(Unknown Source)
at Hack.Gates.CompositeGateClass.<init>(Unknown Source)
at Hack.Gates.GateClass.readHDL(Unknown Source)
at Hack.Gates.GateClass.getGateClass(Unknown Source)
at Hack.Gates.CompositeGateClass.readParts(Unknown Source)
at Hack.Gates.CompositeGateClass.<init>(Unknown Source)
at Hack.Gates.GateClass.readHDL(Unknown Source)
...

And I don't know if I'm getting this error because the testing program is malfunctioning or because my code is wrong and the software can't load it up.

Mark Alexa
  • 125
  • 2
  • 9

3 Answers3

1

It may be helpful to examine the truth tables for Nand and And:

Nand
a | b | out
0 | 0 | 1
0 | 1 | 1
1 | 0 | 1
1 | 1 | 0

And
a | b | out
0 | 0 | 0
0 | 1 | 0
1 | 0 | 0
1 | 1 | 1

And is the inverse of Nand, meaning that for every combination of inputs, And will give the opposite output of Nand. Another way to think of the "opposite" of a binary value is "not" that value.

If you send 2 inputs through a Nand gate and then send its output through a Not gate, you will have Not(Nand(a, b)), which is equivalent to And(a, b).

Antonio Dangond
  • 149
  • 1
  • 8
0

Your problem is that you are trying to use parts (Not, And and Or) that haven't been defined yet (and you are trying to use an And gate in your definition of an And gate).

At each point in the course, you can only use parts that you have previously built. If memory serves, at this point the only part you have available is a Nand gate.

You should be able to construct an And gate using only Nand gates.

MadOverlord
  • 1,034
  • 6
  • 11
0

You're overthinking the problem significantly

If one is given NAND, or (not) AND, then AND can be constructed as (not)NAND since (not)(not) AND = AND

  • I'm still struggling with it. Interestingly, I build websites where I use boolean logic every day, but I burn when learning more fundamental stuff. – Mark Alexa Mar 07 '18 at 16:07
  • From the book I understand that the function returns 1 (True) if both inputs are True too, otherwise returns 0 or False but how to actually write that in HDL language ? I see tutorial here but I just don't get it. Too bad I can't do it in Python. – Mark Alexa Mar 07 '18 at 16:11