← SEARCHABLE ARCHIVE
Nand2Tetris · 2026-05-17

Nand2Tetris — Chapter 2

Boolean Arithmetic and the ALU

INTERACTIVE READING MODE →

//1-bit half adder
//Input: a, b (1 bit)
//Output: sum (LSB of a+b), carry (MSB of a+b)
//Action: sum = a XOR b, carry = a AND b
CHIP HalfAdder {
    IN a, b;
    OUT sum,    // Right bit of a + b
        carry;  // Left bit of a + b

    PARTS:
    Xor(a=a, b=b, out=sum);
    And(a=a, b=b, out=carry);
}
//1-bit full adder
//Input: a, b, c (1 bit input + carry)
//Output: sum (LSB of a+b+c), carry (MSB of a+b+c)
//Action: Calculate (a+b) first, then add c to the result.
CHIP FullAdder {
    IN a, b, c;
    OUT sum,    // Right bit of a + b + c
        carry;  // Left bit of a + b + c

    PARTS:
    //First HalfAdder: calculate a + b
    HalfAdder(a=a, b=b, sum=sum1, carry=carry1);

    //Second HalfAdder: calculate sum1 + c
    HalfAdder(a=sum1, b=c, sum=sum, carry=carry2);

    //final carry: carry1 OR carry2
    Or(a=carry1, b=carry2, out=carry);
}
//16-bit adder (ripple carry method)
//Input: a[16], b[16] (16-bit number, 2's complement representation)
//Output: out[16] (result of a + b, ignoring top carry)
//Operation: Use HalfAdder for 0th, FullAdder for 1~15th.
CHIP Add16 {
    IN a[16], b[16];
    OUT out[16];

    PARTS:
    //Bit 0: No input carry
    HalfAdder(a=a[0], b=b[0],
              sum=out[0], carry=carry1);

    //Bits 1~15: Receive carry input from previous step
    FullAdder(a=a[1], b=b[1], c=carry1,
              sum=out[1], carry=carry2);
    FullAdder(a=a[2], b=b[2], c=carry2,
              sum=out[2], carry=carry3);
    FullAdder(a=a[3], b=b[3], c=carry3,
              sum=out[3], carry=carry4);
    FullAdder(a=a[4], b=b[4], c=carry4,
              sum=out[4], carry=carry5);
    FullAdder(a=a[5], b=b[5], c=carry5,
              sum=out[5], carry=carry6);
    FullAdder(a=a[6], b=b[6], c=carry6,
              sum=out[6], carry=carry7);
    FullAdder(a=a[7], b=b[7], c=carry7,
              sum=out[7], carry=carry8);
    FullAdder(a=a[8], b=b[8], c=carry8,
              sum=out[8], carry=carry9);
    FullAdder(a=a[9], b=b[9], c=carry9,
              sum=out[9], carry=carry10);
    FullAdder(a=a[10], b=b[10], c=carry10,
              sum=out[10], carry=carry11);
    FullAdder(a=a[11], b=b[11], c=carry11,
              sum=out[11], carry=carry12);
    FullAdder(a=a[12], b=b[12], c=carry12,
              sum=out[12], carry=carry13);
    FullAdder(a=a[13], b=b[13], c=carry13,
              sum=out[13], carry=carry14);
    FullAdder(a=a[14], b=b[14], c=carry14,
              sum=out[14], carry=carry15);
    FullAdder(a=a[15], b=b[15], c=carry15,
              sum=out[15]);
}
//16-bit increaser
//input: in[16]
//Output: result of in + 1
//Action: Add 1 to in using Add16
CHIP Inc16 {
    IN in[16];
    OUT out[16];

    PARTS:
    //in + 1 calculation (1 to binary: b[0]=1, remainder=0)
    Add16(a=in,
          b[0]=true,  b[1]=false, b[2]=false, b[3]=false,
          b[4]=false, b[5]=false, b[6]=false, b[7]=false,
          b[8]=false, b[9]=false, b[10]=false, b[11]=false,
          b[12]=false, b[13]=false, b[14]=false, b[15]=false,
          out=out);
}
//ALU (Arithmetic Logic Unit)
//Input: x[16], y[16] (16-bit input)
//zx, nx, zy, ny, f, no (control signals)
//Output: out[16] (operation result)
//zr (1 if out==0, 0 otherwise)
//ng (1 if out<0, 0 otherwise)
//
//Action flow:
//Step 1: If zx=1, x=0, if nx=1, x=NOT(x)
//Step 2: If zy=1, y=0, if ny=1, y=NOT(y)
//Step 3: If f=1, out=x+y (Add16), if f=0, out=x&y (And16)
//Step 4: If no=1 then out=NOT(out)
//Step 5: Calculate zr and ng flags
CHIP ALU {
    IN
        x[16], y[16],  // 16-bit inputs
        zx,             // zero x?
        nx,             // negate x?
        zy,             // zero y?
        ny,             // negate y?
        f,              // (x+y) or (x&y)?
        no;             // negate output?
    OUT
        out[16],        // 16-bit output
        zr,             // is out == 0?
        ng;             // is out < 0?

    PARTS:
    //Step 1: x preprocessing (zero, negate)
    Mux16(a=x, b=false, sel=zx, out=xZero);
    Not16(in=xZero, out=xNot);
    Mux16(a=xZero, b=xNot, sel=nx, out=xProc);

    //Step 2: Preprocess y (zero, negate)
    Mux16(a=y, b=false, sel=zy, out=yZero);
    Not16(in=yZero, out=yNot);
    Mux16(a=yZero, b=yNot, sel=ny, out=yProc);

    //Step 3: Select operation (ADD or AND)
    Add16(a=xProc, b=yProc, out=addRes);
    And16(a=xProc, b=yProc, out=andRes);
    Mux16(a=andRes, b=addRes, sel=f, out=fRes);

    //Step 4: Output post-processing (negate) + flags
    Not16(in=fRes, out=fNot);
    Mux16(a=fRes, b=fNot, sel=no,
          out=out, out[15]=ng, out[0..7]=outLow, out[8..15]=outHigh);

    //Step 5: Calculate zr flag (OR of all bits, then NOT)
    Or8Way(in=outLow, out=orLow);
    Or8Way(in=outHigh, out=orHigh);
    Or(a=orLow, b=orHigh, out=orFinal);
    Not(in=orFinal, out=zr);
}