# NAND2Tetris Note 03 - The Hack ALU

The Airthmetic and Logic Unit is a core part of a computer based on the Von Neumann architecture. It consists of digital circuitry that can perform a variety of arthmetic or logic operations as the name suggests. The Nand2Tetris has a relative simple design for an ALU that can perform a few different functions defined below:

• The ALU Computes one of the following functions:
• x+y, x-y, y-x, 0, 1, -1, x, y, -x, -y, !x, !y,
• x+1, y+1, x-1, y-1, x&y, x|y on two 16-bit inputs,
• according to 6 input bits denoted zx,nx,zy,ny,f,no.
• In addition, the ALU computes two 1-bit outputs:
• if the ALU output == 0, zr is set to 1; otherwise zr is set to 0;
• if the ALU output < 0, ng is set to 1; otherwise ng is set to 0.

The input bits `zx`, `nx`, `zy` and `ny` define the “pre-processing” tasks to be performed on the inputs `x` and `y` prior to computation. `zx` zeros out the whole `x` input if set to one and nx inverts the input (after the effect of `zx` if any) if it is set to true. `zy` and `ny` performs similar actions on the y input. `f` selects between the operations `x & y` (if f == 0) and `x + y` (if f == 1). `no` performs a bitwise inverse on the output of the operation due to `f` if it is set to true. Different combinations of input flags results in one of the 18 operations described above.

The two extra outputs `zr` and `ng` are set based on the characteristics of the final output. This ALU design is specific to the “Hack” computer being developed as part of this course. Other computers may have more options implemented in the ALU. This is a design trade-off as to how many operations need to be implemented in hardware as opposed to software. In case of “Hack”, operations like multiplication and division will be implemented in software later.

• NAND2Tetris