0% found this document useful (0 votes)
14 views113 pages

CS 201 Lecture 3

The document discusses representing numbers using binary and boolean arithmetic. It covers representing numbers with binary, boolean arithmetic, and signed numbers. It also discusses building an Arithmetic Logic Unit and chips as part of a computer system.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views113 pages

CS 201 Lecture 3

The document discusses representing numbers using binary and boolean arithmetic. It covers representing numbers with binary, boolean arithmetic, and signed numbers. It also discusses building an Arithmetic Logic Unit and chips as part of a computer system.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 113

From Nand to Tetris

Building a Modern Computer from First Principles

Lecture 3

Boolean Arithmetic

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 1


Nand to Tetris Roadmap: Hardware
abstraction
machine
language

assembler p6

Building a
hardware platform
abstraction
computer
abstraction Building
computer
p4 chips
ALU, RAM abstraction Building
p5 p2 gates
elementary
logic gates
p3 p1

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 2


Nand to Tetris Roadmap: Hardware
abstraction
machine
language

assembler p6

Building a
hardware platform
abstraction
computer
abstraction Building
computer
p4 chips
ALU, RAM abstraction Building
p5 p2 gates
elementary
logic gates
p3 p1

Project 1
Build 15 elementary logic gates

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 3


Nand to Tetris Roadmap: Hardware
abstraction
machine
language

assembler p6

Building a
hardware platform
abstraction
computer
abstraction Building
computer
p4 chips
ALU, RAM abstraction Building
p5 p2 gates
elementary
logic gates
p3 p1

Project 2
Building chips that do arithmetic,
ending up with an ALU
Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 4
Computer system

CPU

ALU
Input Memory Output

Registers

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 5


Computer system

CPU

ALU
Input Memory Output

Registers

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 6


Arithmetic Logical Unit
f
x

f (x , y ) The ALU computes a given function


ALU on two given n-bit values, and
outputs an n-bit value
y

ALU functions ( f )
• Arithmetic: x + y, x – y, x + 1, x – 1, ...
• Logical: x & y, x | y, !x , ...

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 7


Arithmetic Logical Unit
(40521)
+
1001111001001001
(40538)
1001111001011010 The ALU computes a given function
(17) ALU on two given n-bit values, and
0000000000010001
outputs an n-bit value

ALU functions ( f )
• Arithmetic: x + y, x – y, x + 1, x – 1, ...
• Logical: x & y, x | y, !x , ...

Challenges
• Use 0’s and 1’s for representing numbers
• Use logic gates for realizing arithmetic functions.

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 8


Chapter 2: Boolean Arithmetic

Theory Practice

• Representing numbers • Arithmetic Logic Unit (ALU)

• Binary numbers • Project 2: Chips

• Boolean arithmetic • Project 2: Guidelines

• Signed numbers

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 9


Chapter 2: Boolean Arithmetic

Theory Practice

• Representing numbers • Arithmetic Logic Unit (ALU)

• Binary numbers • Project 2: Chips

• Boolean arithmetic • Project 2: Guidelines

• Signed numbers

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 10


Representation

This is not a pipe


(by René Magritte)

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 11


Representation

17
This is not seventeen.

Rather, it’s an agreed-upon code (numeral)


that represents the number seventeen.

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 12


A brief history of numeral systems

Twenty seven
goats

Unary:

Egyptian:

Roman: XXVII

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 13


A brief history of numeral systems

Six thousands,
... five hundreds,
and seven goats

Unary: ...

Egyptian:
Old numeral systems:
• Don’t scale
Roman: MMMMMMDVII
• Cumbersome arithmetic
• Used until about 1000 years ago
• Blocked the progress of Algebra
(and commerce, science, technology)

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 14


Positional numeral system

Six thousands,
... five hundreds,
and seven goats

3 2 1 0

6507

𝑛−1

෍ 𝑑𝑖 ∙ 10𝑖 = 6 ∙ 103 + 5 ∙ 102 + 0 ∙ 101 + 7 ∙ 100 = 6507


0
A most important innovation, brought
Where n is the to the West from the East around 1200
number of Positional representation
digits in the
numeral, and di • Digits: A fixed set of symbols, including 0
is the digit in The method mentions
position i • Base: The number of symbols no specific base.
• Numeral: An ordered sequence of digits
• Value: The digit in position i (counting from right to left, and starting at 0)
encodes how many copies of base i are added to the value.

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 15


Chapter 2: Boolean Arithmetic

Theory Practice

• Representing numbers • Arithmetic Logic Unit (ALU)

• Binary numbers • Project 2: Chips

• Boolean arithmetic • Project 2: Guidelines

• Representing signed numbers

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 16


Positional number system

Seven thousands
... and fifty three
goats

3 2 1 0
7 0 5 3 10

𝑛−1

෍ 𝑑𝑖 ∙ 10𝑖 = 7 ∙ 103 + 0 ∙ 102 + 5 ∙ 101 + 3 ∙ 100 = 7053


0

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 17


Positional number system

Seven thousands
... and fifty three
goats

Decimal (base 10) system: 3 2 1 0


Human friendly 7 0 5 3 10

𝑛−1

෍ 𝑑𝑖 ∙ 10𝑖 = 7 ∙ 103 + 0 ∙ 102 + 5 ∙ 101 + 3 ∙ 100 = 7053


0

12 11 10 . . . 3 2 1 0
Binary (base 2) system: 1 1 0 1 1 1 0 0 0 1 1 0 12
Computer friendly
. . .
𝑛−1

෍ 𝑑𝑖 ∙ 2𝑖 = 1 ∙ 212 + 1 ∙ 211 + 0 ∙ 210 + … + 1 ∙ 2 = 7053


0

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 18


Binary and decimal systems
Binary Decimal
0 0
1 1
1 0 2
1 1 3
1 0 0 4 Humans are used to enter and view numbers in base 10;
1 0 1 5
Computers represent and process numbers in base 2;
1 1 0 6
Therefore, we need efficient algorithms for converting
1 1 1 7
from one base to the other.
1 0 0 0 8
1 0 0 1 9
1 0 1 0 10
1 0 1 1 11
1 1 0 0 12
1 1 0 1 13
... ...
Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 19
Decimal binary conversions

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 20


Decimal binary conversions
Powers of 2: (aids in calculations)
20 = 1 Binary to decimal:
21 = 2 5 4 3 2 1 0
22 = 4 decimal ( 11010 1 2 ( = 25 + 24 +22 +20 = 53 10
23 = 8
24 = 16
Decimal to binary:
25 = 32
5 4 3 2 1 0
26 = 64 binary ( 5 3 10 ) = 25 + 24 +22 +20 = 1 1 0 1 0 12
27 = 128
28 = 256 Algorithm: What is the largest power of 2 that “fits into” 53? It’s 32 = 25.
29 = 512 We still have to handle 53 – 32, so, what is the largest power of 2 that fits
into 21? It’s 16 = 24, and so on.
210 = 1024
...
Practice:

decimal ( 1011010 2 ( = ?
binary ( 5 2 310 ( = ?
Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 21
Decimal binary conversions
Powers of 2: (aids in calculations)
20 = 1 Binary to decimal:
21 = 2 5 4 3 2 1 0
22 = 4 decimal ( 11010 1 2 ( = 25 + 24 +22 +20 = 53 10
23 = 8
24 = 16
Decimal to binary:
25 = 32
5 4 3 2 1 0
26 = 64 binary ( 5 3 10 ) = 25 + 24 +22 +20 = 1 1 0 1 0 12
27 = 128
28 = 256 Algorithm: What is the largest power of 2 that “fits into” 53? It’s 32 = 25.
29 = 512 We still have to handle 53 – 32, so, what is the largest power of 2 that fits
into 21? It’s 16 = 24, and so on.
210 = 1024
...
Practice:

decimal ( 1011010 2 ( = 9 0 10

binary ( 5 2 310 ( = 100000 1011 2


Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 22
The binary system

Inside computers,
everything is binary

G.W. Leibnitz
(1646 – 1716)
Worshipped
binary numbers

Binary numerals are easy to:


❑ Compare ❑ Store
❑ Add ❑ Transmit
❑ Subtract ❑ Verify
❑ Multiply ❑ Correct
❑ Divide ❑ Compress
❑ ... ❑ ...
Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 23
Chapter 2: Boolean Arithmetic

Theory Practice

• Representing numbers • Arithmetic Logic Unit (ALU)

• Binary numbers • Project 2: Chips

• Boolean arithmetic • Project 2: Guidelines

• Signed numbers

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 24


Boolean arithmetic

We have to figure out efficient ways to perform, on binary numbers:

• Addition We’ll implement it using logic gates

• Subtraction
We’ll get it for free

• Multiplication
Based on addition
• Division

Addition is the foundation of all arithmetic operations.

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 25


Addition

0 0 1 0 0 1 1 0
1 0 1 0 7 8 7 5
+ +
1 1 5 6 2

1 1 0 1 8 4 3 7

Binary addition Decimal addition

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 26


Addition

0 0 1 0
1 0 1 0
+
1 1

1 1 0 1

Binary addition

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 27


Addition
Computers represent integers using a fixed number of bits,
sometimes called “word size”. For example, let’s assume n = 4:

0 0 1 0
1 0 1 0 0 0 0 1
+ +
0 0 1 1 0 1 0 1

1 1 0 1

Binary addition Another example

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 28


Addition
Computers represent integers using a fixed number of bits,
sometimes called “word size”. For example, let’s assume n = 4:

0 0 1 0 0 0 1

1 0 1 0 0 0 0 1
+ +
0 0 1 1 0 1 0 1

1 1 0 1 0 1 1 0

Binary addition Another example

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 29


Addition
Computers represent integers using a fixed number of bits,
sometimes called “word size”. For example, let’s assume n = 4:

0 0 1 0 0 0 1

1 0 1 0 0 0 0 1 0 1 1 1
+ + +
0 0 1 1 0 1 0 1 1 1 1 0

1 1 0 1 0 1 1 0

Binary addition Another example Another example

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 30


Addition
Computers represent integers using a fixed number of bits,
sometimes called “word size”. For example, let’s assume n = 4:

0 0 1 0 0 0 1 1 1 1 0

1 0 1 0 0 0 0 1 0 1 1 1
+ + +
0 0 1 1 0 1 0 1 1 1 1 0

1 1 0 1 0 1 1 0 1 0 1 0 1

Binary addition Another example Overflow

Handling overflow
• Our decision: Ignore it
• As we will soon see, ignoring the overflow bit is not a bug, it’s a feature.

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 31


Addition
Word size n = 16, 32, 64, …

0 ... 0 0 0 0 0 1 1 0 1 1 1 0 0 0

0 ... 0 0 0 0 0 0 1 1 0 1 0 1 0 1 Same
+ 0 ... 0 0 0 0 0 0 0 1 0 1 1 1 0 0
addition
algorithm
for any n
0 ... 0 0 0 0 0 1 0 0 1 1 0 0 0 1

Hardware implementation
We’ll build an Adder chip that
implements this addition algorithm,
Using the chips built in project 1.
How? Later.

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 32


Chapter 2: Boolean Arithmetic

Theory Practice

• Representing numbers • Arithmetic Logic Unit (ALU)

• Binary numbers • Project 2: Chips

• Boolean arithmetic (addition) • Project 2: Guidelines

• Signed numbers

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 33


Chapter 2: Boolean Arithmetic

Theory Practice

• Representing numbers • Arithmetic Logic Unit (ALU)

• Binary numbers • Project 2: Chips

• Boolean arithmetic (addition) • Project 2: Guidelines

• Signed numbers
(x + y, –x + y, x + –y, –x + –y)

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 34


Pascal’s Adder

https://www.youtube.com/watch?v=3h71HAJWnVU

35
Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 35
Subtracting using Pascal’s Adder
• 9’s Complement
❑ 𝐶𝑃 𝐴 = 10𝑛 − 1 − 𝐴
❑ 𝐶𝑃(𝐶𝑃 𝐴 ) = 𝐴
• Subtraction using 9’s Complement
❑ 𝐶𝑃 𝐴 − 𝐵 = 10𝑛 − 1 − 𝐴 + 𝐵 = 𝐶𝑃 𝐴 + 𝐵
❑ 𝐶𝑃 𝐶𝑃 𝐴 + 𝐵 = 𝐴 − 𝐵

36
Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 36
Signed integers
• Positive
• 0
• Negative

In most programming languages, the short, int, and long data types use 16, 32,
and 64 bits for representing signed integers
Arithmetic operations on signed integers (x op y, –x op y, x op –y, –x op –y,
where op = {+, –, *, /}) are by far what computers do most of the time

Therefore …
Efficient algorithms for handling arithmetic operations on signed integers hold
the key to building efficient computers.

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 37


Signed integers
code(x) x
0000 0 0
This particular example: word size is n = 4
0001 1 1
0010 2 2 In general, n bits allow representing all the unsigned
0011 3 3 integers 0 … 2n – 1
0100 4 4
0101 5 5
0110 6 6 What about negative numbers?
0111 7 7
1000 8 8
We can use half of the code space for representing
1001 9 9
positive numbers, and the other half for negatives.
1010 10 10
1011 11 11
1100 12 12
1101 13 13
1110 14 14
1111 15 15

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 38


Signed integers
code(x) x
0000 0 0 Representation:
0001 1 1
0010 2 2 Left-most bit (MSB): Represents the sign, +/-
0011 3 3 Remaining bits: Represent a positive integer
0100 4 4
0101 5 5
Issues
0110 6 6
0111 7 7 • – 0: Huh?
1000 8 –0
• code(x) + code(– x) ≠ code(0)
1001 9 –1
1010 10 –2 • The codes are not monotonically increasing
1011 11 –3
1100 12 –4 • more complications.
1101 13 –5
1110 14 –6
1111 15 –7

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 39


Two’s complement
code(x) x
The representation
0000 0 0
• Assumption: Word size = n bits
0001 1 1
• The “two’s complement” of x is defined to be 2n – x
0010 2 2
• The negative of x is coded by the two’s complement of x
0011 3 3
0100 4 4
0101 5 5 From decimal to binary:
0110 6 6
if x ≥ 0 return binary(x)
0111 7 7
else return binary(2n – x)
1000 8 –8
1001 9 –7 From binary to decimal:
1010 10 –6
if MSB = 0 return decimal(bits)
1011 11 –5
1100 12 –4 else return “–” and then (2n – decimal(bits))
1101 13 –3
1110 14 –2
1111 15 –1

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 40


Two’s complement: Addition
code(x) x Compute x + y where x and y are signed
0000 0 0
Algorithm: Regular addition, modulo 2n
0001 1 1
0010 2 2
6 6
0011 3 3 + = +
0100 4 4 –2 14
0101 5 5 20 % 16 = 4 codes 4
0110 6 6
0111 7 7 3 3
+ = +
1000 8 –8 –5 11
1001 9 –7 14 % 16 = 14 codes –2
1010 10 –6
1011 11 –5 –2 14
+ = +
1100 12 –4 –5 11
1101 13 –3
25 % 16 = 9 codes –7
1110 14 –2
1111 15 –1

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 41


Two’s complement: Addition
code(x) x Compute x + y where x and y are signed
0000 0 0
Algorithm: Regular addition, modulo 2n
0001 1 1
0010 2 2
6 6
0011 3 3 + = +
0100 4 4 –2 14
0101 5 5 20 % 16 = 4 codes 4
0110 6 6
Practice:
0111 7 7
1000 8 –8
4
1001 9 –7 +
–7
= ?
1010 10 –6
1011 11 –5
1100 12 –4
1101 13 –3 –2
1110 14 –2
+
–4
= ?
1111 15 –1

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 42


Two’s complement: Addition
code(x) x Compute x + y where x and y are signed
0000 0 0
Algorithm: Regular addition, modulo 2n
0001 1 1
0010 2 2
6 6
0011 3 3 + = +
0100 4 4 –2 14
0101 5 5 20 % 16 = 4 codes 4
0110 6 6
Practice:
0111 7 7
1000 8 –8
4 4
1001 9 –7 + = +
1010 10 –6 –7 9
1011 11 –5 13 % 16 = 13 codes –3
1100 12 –4
1101 13 –3 –2 14
+ = +
1110 14 –2 –4 12
1111 15 –1 26 % 16 = 10 codes –6

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 43


Two’s complement: Addition
code(x) x
At the binary level (same algorithm):
0000 0 0
6 0110
0001 1 1 + = + Ignoring the overflow bit

0010 2 2 –2 1110 is the binary equivalent of


modulo 2n
0011 3 3 10100 codes 4
0100 4 4
0101 5 5 3 0011
+ = +
0110 6 6 –5 1011
0111 7 7 1110 codes –2
1000 8 –8
1001 9 –7 –2 1110
1010 10 –6
+ = +
–5 1011
1011 11 –5
11001 codes –7
1100 12 –4
1101 13 –3
1110 14 –2
1111 15 –1

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 44


Two’s complement: Addition
code(x) x
At the binary level (same algorithm):
0000 0 0
6 0110
0001 1 1 + = +
0010 2 2 –2 1110
0011 3 3 10100 codes 4
0100 4 4 More examples:
0101 5 5 0101
5
0110 6 6 + = +
7 0111
0111 7 7
1000 8 –8 1100 codes –4 5 + 7 = –4 ???
1001 9 –7
–7 1001
1010 10 –6 + = +
–3 1101
1011 11 –5
1100 12 –4 10110 codes 6 –7 + –3 = 6 ???
1101 13 –3
1110 14 –2 Overflow detection
1111 15 –1 When you add up two positives (negatives) and get a negative
(positive) result, you know that you have overflow
Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 45
Two’s complement: Subtraction
code(x) x Compute x – y where x and y are signed
0000 0 0
• x – y is the same as x + (–y)
0001 1 1
0010 2 2 • So… convert y and add up the two values
0011 3 3 (we already know how to add up signed numbers)
0100 4 4
But … How to convert a number (efficiently)?
0101 5 5
0110 6 6
0111 7 7
1000 8 –8
1001 9 –7
1010 10 –6
1011 11 –5
1100 12 –4
1101 13 –3
1110 14 –2
1111 15 –1

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 46


Two’s complement: Sign conversion
code(x) x
Compute –x from x
0000 0 0
0001 1 1 Insight: code(–x) = (2n – x) = 1 + (2n – 1) – x
0010 2 2 = 1 + (1111) – x
0011 3 3
= 1 + flippedBits (x)
0100 4 4
0101 5 5 Algorithm: To convert bbb...b:
0110 6 6 Flip all the bits and add 1 to the result
0111 7 7
1000 8 –8
Example: Convert 0010 (2)
1001 9 –7
1101 (flipped)
1010 10 –6
+ 1
1011 11 –5
1100 12 –4 1110 (–2)
1101 13 –3
1110 14 –2
1111 15 –1

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 47


Two’s complement: Sign conversion
code(x) x
Compute –x from x
0000 0 0
0001 1 1 Insight: code(–x) = (2n – x) = 1 + (2n – 1) – x
0010 2 2 = 1 + (1111) – x
0011 3 3
= 1 + flippedBits (x)
0100 4 4
0101 5 5 Algorithm: To convert bbb...b:
0110 6 6 Flip all the bits and add 1 to the result
0111 7 7
1000 8 –8
Practice: Convert 1010 (–6)
1001 9 –7
1010 10 –6
1011 11 –5
1100 12 –4
1101 13 –3
1110 14 –2
1111 15 –1

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 48


Two’s complement: Sign conversion
code(x) x
Compute –x from x
0000 0 0
0001 1 1 Insight: code(–x) = (2n – x) = 1 + (2n – 1) – x
0010 2 2 = 1 + (1111) – x
0011 3 3
= 1 + flippedBits (x)
0100 4 4
0101 5 5 Algorithm: To convert bbb...b:
0110 6 6 Flip all the bits and add 1 to the result
0111 7 7
1000 8 –8
Practice: Convert 1010 (–6)
1001 9 –7
0101 (flipped)
1010 10 –6
+ 1
1011 11 –5
1100 12 –4 0110 (6)
1101 13 –3
1110 14 –2
1111 15 –1

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 49


Two’s complement: Recap
code(x) x Observations
0000 0 0 • The method represents all the integers in the range
0001 1 1 –2n–1, ..., –1, 0, 1, ..., 2n–1 – 1
0010 2 2 • code(x) + code(– x) = code(0)
0011 3 3
• The codes are monotonically increasing
0100 4 4
0101 5 5 • Arithmetic on signed integers is the same as arithmetic
on unsigned integers
0110 6 6
0111 7 7 • Simple! Elegant! Powerful!
1000 8 –8
1001 9 –7 Implications for hardware designers
1010 10 –6 Arithmetic on signed integers can be implemented
1011 11 –5 using the same hardware used for handling arithmetic of
1100 12 –4 unsigned integers
1101 13 –3
1110 14 –2
1111 15 –1

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 50


Chapter 2: Boolean Arithmetic

Theory Practice

• Representing numbers • Arithmetic Logic Unit (ALU)

• Binary numbers • Project 2: Chips

• Boolean arithmetic • Project 2: Guidelines

• Signed numbers

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 51


Chapter 2: Boolean Arithmetic

Theory Practice

• Representing numbers • Arithmetic Logic Unit (ALU)

• Binary numbers • Project 2: Chips

• Boolean arithmetic • Project 2: Guidelines

• Signed numbers

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 52


Von Neumann Architecture
Computer System

CPU

ALU
Input Memory Output

Registers

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 53


The Arithmetic Logical Unit

f
The ALU computes a given
function on its two given data input1
inputs, and outputs the result
f (input1, input2)
f : one out of a family of ALU
pre-defined arithmetic functions
input2
(add, subtract, multiply…) and
logical functions (And, Or, Xor, …)

Design issue: Which functions should the ALU perform?


A hardware / software tradeoff: Any function not implemented by the ALU
can be implemented later in system software
• Hardware implementations: Faster, and more expensive
• Software implementations: Slower, less expensive

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 54


The Hack ALU
• Operates on two 16-bit, two’s complement values

zx nx zy ny f no

x
16 bits
ALU 16 bits
out
y
16 bits

zr ng

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 55


The Hack ALU
• Operates on two 16-bit, two’s complement values
• Outputs a 16-bit, two’s complement value
out
0
1
-1
x
zx nx zy ny f no y
!x
!y
-x
-y
x
x+1
16 bits
y+1
ALU 16 bits
out
x-1
y
16 bits y-1
x+y
x-y
y-x
zr ng x&y
x|y

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 56


The Hack ALU
• Operates on two 16-bit, two’s complement values
• Outputs a 16-bit, two’s complement value
• Also outputs two 1-bit values (later) out
0
1
-1
x
zx nx zy ny f no y
!x
!y
-x
-y
x
x+1
16 bits
y+1
ALU 16 bits
out
x-1
y
16 bits y-1
x+y
x-y
y-x
zr ng x&y
x|y

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 57


The Hack ALU
• Operates on two 16-bit, two’s complement values
• Outputs a 16-bit, two’s complement value
• Also outputs two 1-bit values (later) out
0
• Which function to compute is set by six 1-bit inputs 1
-1
x
zx nx zy ny f no y
!x
!y
-x
-y
x
x+1
16 bits
y+1
ALU 16 bits
out
x-1
y
16 bits y-1
x+y
x-y
y-x
zr ng x&y
x|y

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 58


The Hack ALU

To cause the ALU to compute a function: control bits


Set the control bits to one of the binary
zx nx zy ny f no out
combinations listed in the table. 1 0 1 0 1 0 0
1 1 1 1 1 1 1
1 1 1 0 1 0 -1
0 0 1 1 0 0 x
zx nx zy ny f no 1 1 0 0 0 0 y
0 0 1 1 0 1 !x
1 1 0 0 0 1 !y
0 0 1 1 1 1 -x
1 1 0 0 1 1 -y
x
0 1 1 1 1 1 x+1
16 bits
1 1 0 1 1 1 y+1
ALU 16 bits
out
0 0 1 1 1 0 x-1
y
16 bits 1 1 0 0 1 0 y-1
0 0 0 0 1 0 x+y
0 1 0 0 1 1 x-y
0 0 0 1 1 1 y-x
zr ng 0 0 0 0 0 0 x&y
0 1 0 1 0 1 x|y

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 59


The Hack ALU in action

To cause the ALU to compute a function: control bits


Set the control bits to one of the binary
zx nx zy ny f no out
combinations listed in the table. 1 0 1 0 1 0 0
1 1 1 1 1 1 1
1 1 1 0 1 0 -1
0 0 1 1 0 0 x
zx nx zy ny f no 1 1 0 0 0 0 y
0 0 1 1 0 1 !x
1 1 0 0 0 1 !y
0 0 1 1 1 1 -x
1 1 0 0 1 1 -y
x
0 1 1 1 1 1 x+1
16 bits
1 1 0 1 1 1 y+1
ALU 16 bits
out
0 0 1 1 1 0 x-1
y
16 bits 1 1 0 0 1 0 y-1
0 0 0 0 1 0 x+y
0 1 0 0 1 1 x-y
0 0 0 1 1 1 y-x
zr ng 0 0 0 0 0 0 x&y
0 1 0 1 0 1 x|y

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 60


The Hack ALU in action: Compute y-x

To cause the ALU to compute a function: control bits


Set the control bits to one of the binary
zx nx zy ny f no out
combinations listed in the table. 1 0 1 0 1 0 0
1 1 1 1 1 1 1
1 1 1 0 1 0 -1
0 0 1 1 0 0 x
zx nx zy ny f no 1 1 0 0 0 0 y
0 0 1 1 0 1 !x
1 1 0 0 0 1 !y
0 0 1 1 1 1 -x
1 1 0 0 1 1 -y
x
0 1 1 1 1 1 x+1
16 bits
1 1 0 1 1 1 y+1
ALU 16 bits
out
0 0 1 1 1 0 x-1
y
16 bits 1 1 0 0 1 0 y-1
0 0 0 0 1 0 x+y
0 1 0 0 1 1 x-y
0 0 0 1 1 1 y-x
zr ng 0 0 0 0 0 0 x&y
0 1 0 1 0 1 x|y

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 61


The Hack ALU in action: Compute y-x

2. Evaluate the chip logic

Load 3. Inspect the


tools/builtInChips/ALU.hdl ALU outputs

1. Set the ALU’s inputs and control


bits to some test values
The built-in ALU
(000111 codes “output y-x”(
implementation has
GUI side-effects

Built-in ALU
implementation

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 62


The Hack ALU in action

To cause the ALU to compute a function: control bits


Set the control bits to one of the binary
zx nx zy ny f no out
combinations listed in the table. 1 0 1 0 1 0 0
1 1 1 1 1 1 1
1 1 1 0 1 0 -1
0 0 1 1 0 0 x
zx nx zy ny f no 1 1 0 0 0 0 y
0 0 1 1 0 1 !x
1 1 0 0 0 1 !y
0 0 1 1 1 1 -x
1 1 0 0 1 1 -y
x
0 1 1 1 1 1 x+1
16 bits
1 1 0 1 1 1 y+1
ALU 16 bits
out
0 0 1 1 1 0 x-1
y
16 bits 1 1 0 0 1 0 y-1
0 0 0 0 1 0 x+y
0 1 0 0 1 1 x-y
0 0 0 1 1 1 y-x
zr ng 0 0 0 0 0 0 x&y
0 1 0 1 0 1 x|y

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 63


The Hack ALU in action: Compute x & y

To cause the ALU to compute a function: control bits


Set the control bits to one of the binary
zx nx zy ny f no out
combinations listed in the table. 1 0 1 0 1 0 0
1 1 1 1 1 1 1
1 1 1 0 1 0 -1
0 0 1 1 0 0 x
zx nx zy ny f no 1 1 0 0 0 0 y
0 0 1 1 0 1 !x
1 1 0 0 0 1 !y
0 0 1 1 1 1 -x
1 1 0 0 1 1 -y
x
0 1 1 1 1 1 x+1
16 bits
1 1 0 1 1 1 y+1
ALU 16 bits
out
0 0 1 1 1 0 x-1
y
16 bits 1 1 0 0 1 0 y-1
0 0 0 0 1 0 x+y
0 1 0 0 1 1 x-y
0 0 0 1 1 1 y-x
zr ng 0 0 0 0 0 0 x&y
0 1 0 1 0 1 x|y

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 64


The Hack ALU in action: Compute x & y

Set to binary
I/O format

Inspect the
ALU outputs

Set the ALU’s inputs and control


bits to some test values
(000000 codes “compute x&y”(

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 65


The Hack ALU operation

zx nx zy ny f no

x
16 bits
ALU 16 bits
out
y
16 bits

zr ng

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 66


The Hack ALU operation
pre-setting pre-setting selecting between post-setting Resulting
the x input the y input computing + or & the output ALU output
zx nx zy ny f no out

if zx if nx if zy if ny if f if no
then then then then then out=x+y then
x=0 x=!x y=0 y=!y else out=x&y out=!out out(x,y)=

zx nx zy ny f no

x
16 bits
ALU 16 bits
out
y
16 bits

zr ng

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 67


The Hack ALU operation
pre-setting pre-setting selecting between post-setting Resulting
the x input the y input computing + or & the output ALU output
zx nx zy ny f no out

if zx if nx if zy if ny if f if no
then then then then then out=x+y then
x=0 x=!x y=0 y=!y else out=x&y out=!out out(x,y)=

zx nx zy ny f no

x
16 bits
ALU 16 bits
out
y
16 bits

zr ng

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 68


The Hack ALU operation
pre-setting pre-setting selecting between post-setting Resulting
the x input the y input computing + or & the output ALU output
zx nx zy ny f no out

if zx if nx if zy if ny if f if no
then then then then then out=x+y then
x=0 x=!x y=0 y=!y else out=x&y out=!out out(x,y)=

zx nx zy ny f no

x
16 bits
ALU 16 bits
out
y
16 bits

zr ng

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 69


The Hack ALU operation
pre-setting pre-setting selecting between post-setting Resulting
the x input the y input computing + or & the output ALU output
zx nx zy ny f no out

if zx if nx if zy if ny if f if no
then then then then then out=x+y then
x=0 x=!x y=0 y=!y else out=x&y out=!out out(x,y)=

zx nx zy ny f no

x
16 bits
ALU 16 bits
out
y
16 bits

zr ng

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 70


The Hack ALU operation
pre-setting pre-setting selecting between post-setting Resulting
the x input the y input computing + or & the output ALU output
zx nx zy ny f no out

if zx if nx if zy if ny if f if no
then then then then then out=x+y then
x=0 x=!x y=0 y=!y else out=x&y out=!out out(x,y)=

zx nx zy ny f no

x
16 bits
ALU 16 bits
out
y
16 bits

zr ng

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 71


The Hack ALU operation
pre-setting pre-setting selecting between post-setting Resulting
the x input the y input computing + or & the output ALU output
zx nx zy ny f no out

if zx if nx if zy if ny if f if no
then then then then then out=x+y then
x=0 x=!x y=0 y=!y else out=x&y out=!out out(x,y)=
1 0 1 0 1 0 0
1 1 1 1 1 1 1
1 1 1 0 1 0 -1
0 0 1 1 0 0 x
1 1 0 0 0 0 y
0 0 1 1 0 1 !x
1 1 0 0 0 1 !y
0 0 1 1 1 1 -x
1 1 0 0 1 1 -y
0 1 1 1 1 1 x+1
1 1 0 1 1 1 y+1
0 0 1 1 1 0 x-1
1 1 0 0 1 0 y-1
0 0 0 0 1 0 x+y
0 1 0 0 1 1 x-y
0 0 0 1 1 1 y-x
0 0 0 0 0 0 x&y
0 1 0 1 0 1 x|y

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 72


The Hack ALU operation: Compute !x
pre-setting pre-setting selecting between post-setting Resulting
the x input the y input computing + or & the output ALU output
zx nx zy ny f no out

if zx if nx if zy if ny if f if no
then then then then then out=x+y then
x=0 x=!x y=0 y=!y else out=x&y out=!out out(x,y)=
1 0 1 0 1 0 0
1 1 1 1 1 1 1
1 1 1 0 1 0 -1
0 0 1 1 0 0 x
1 1 0 0 0 0 y
0 0 1 1 0 1 !x
1 1 0 0 0 1 !y
0 0 1 Example:
1 1 !x
compute 1 -x
1 1 0 0 1 1 -y
x: 1 1 0 0
0 1 1 1 1 1 x+1
y: 1 0 1 1 (irrelevant)
1 1 0 1 1 1 y+1
0 0 1 1 1 0 x-1
1 1 0 0 1 0 y-1
0 0 0 0 1 0 x+y
0 1 0 0 1 1 x-y
0 0 0 1 1 1 y-x
0 0 0 0 0 0 x&y
0 1 0 1 0 1 x|y

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 73


The Hack ALU operation: Compute !x
pre-setting pre-setting selecting between post-setting Resulting
the x input the y input computing + or & the output ALU output
zx nx zy ny f no out

if zx if nx if zy if ny if f if no
then then then then then out=x+y then
x=0 x=!x y=0 y=!y else out=x&y out=!out out(x,y)=
1 0 1 0 1 0 0
1 1 1 1 1 1 1
1 1 1 0 1 0 -1
0 0 1 1 0 0 x
1 1 0 0 0 0 y
0 0 1 1 0 1 !x
1 1 0 0 0 1 !y
0 0 1 1
Example: compute1 !x 1 -x
1 1 0 0 1 1 -y
x: 1 1 0 0
0 1 1 1 1 1 x+1
y: 1 0 1 1 (irrelevant)
1 1 0 1 1 1 y+1
0 0 1 Following
1 pre-setting:
1 0 x-1
1 1 0 x: 0 1 1 01 0 0 y-1
0 0 0 y: 0 1 1 11 1 0 x+y
0 1 0 0 1 1 x-y
Computation and post-setting:
0 0 0 1 1 1 y-x
0 0 0 x&y: 0 1 1 00 0 0 x&y
0 1 0 !(x&y):
1 0 0 10 1 (!x) 1 x|y

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 74


The Hack ALU operation
pre-setting pre-setting selecting between post-setting Resulting
the x input the y input computing + or & the output ALU output
zx nx zy ny f no out

if zx if nx if zy if ny if f if no
then then then then then out=x+y then
x=0 x=!x y=0 y=!y else out=x&y out=!out out(x,y)=
1 0 1 0 1 0 0
1 1 1 1 1 1 1
1 1 1 0 1 0 -1
0 0 1 1 0 0 x
1 1 0 0 0 0 y
0 0 1 1 0 1 !x
1 1 0 0 0 1 !y
0 0 1 1 1 1 -x
1 1 0 0 1 1 -y
0 1 1 1 1 1 x+1
1 1 0 1 1 1 y+1
0 0 1 1 1 0 x-1
1 1 0 0 1 0 y-1
0 0 0 0 1 0 x+y
0 1 0 0 1 1 x-y
0 0 0 1 1 1 y-x
0 0 0 0 0 0 x&y
0 1 0 1 0 1 x|y

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 75


The Hack ALU operation: Compute y-x
pre-setting pre-setting selecting between post-setting Resulting
the x input the y input computing + or & the output ALU output
zx nx zy ny f no out

if zx if nx if zy if ny if f if no
then then then then then out=x+y then
x=0 x=!x y=0 y=!y else out=x&y out=!out out(x,y)=
1 0 1 0 1 0 0
1 1 1 1 1 1 1
1 1 1 0 1 0 -1
0 0 1 1 Example:
0 compute y-x
0 x
1 1 0 0 x: 0 0 0 1 0 0(2) y
0 0 1 1 y: 0 0 1 1 1 1(7) !x
1 1 0 0 0 1 !y
0 0 1 1 1 1 -x
1 1 0 0 1 1 -y
0 1 1 1 1 1 x+1
1 1 0 1 1 1 y+1
0 0 1 1 1 0 x-1
1 1 0 0 1 0 y-1
0 0 0 0 1 0 x+y
0 1 0 0 1 1 x-y
0 0 0 1 1 1 y-x
0 0 0 0 0 0 x&y
0 1 0 1 0 1 x|y

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 76


The Hack ALU operation: Compute y-x
pre-setting pre-setting selecting between post-setting Resulting
the x input the y input computing + or & the output ALU output
zx nx zy ny f no out

if zx if nx if zy if ny if f if no
then then then then then out=x+y then
x=0 x=!x y=0 y=!y else out=x&y out=!out out(x,y)=
1 0 1 0 1 0 0
1 1 1 1 1 1 1
1 1 1 0 1 0 -1
0 0 1 1 Example:
0 compute y-x0 x
1 1 0 0 x: 0 0 0 1 0 0(2) y
0 0 1 1 y: 0 0 1 1 1 1(7) !x
1 1 0 0 0 1 !y
Following pre-setting:
0 0 1 1 1 1 -x
1 1 0 0 x: 1 0 0 1 01 -y
0 1 1 1 y: 1 1 0 0 01 x+1
1 1 0 1 1
Computation 1
and post-setting:y+1
0 0 1 1 1 0 x-1
x+y: 1 0 1 0
1 1 0 0 1 0 y-1
!(x+y): 0 1 0 1 (5)
0 0 0 0 1 0 x+y
0 1 0 0 1 1 x-y
0 0 0 1 1 1 y-x
0 0 0 0 0 0 x&y
0 1 0 1 0 1 x|y

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 77


The Hack ALU operation
pre-setting pre-setting selecting between post-setting Resulting
the x input the y input computing + or & the output ALU output
zx nx zy ny f no out

if zx if nx if zy if ny if f if no
then then then then then out=x+y then
x=0 x=!x y=0 y=!y else out=x&y out=!out out(x,y)=
1 0 1 0 1 0 0
1 1 1 1 1 1 1
1 1 1 0 1 0 -1
0 0 1 1 0 0 x
1 1 0 0 0 0 y
0 0 1 1 0 1 !x
1 1 0 0 0 1 !y
0 0 1 1 1 1 -x
1 1 0 0 1 1 -y
0 1 1 1 1 1 x+1
1 1 0 1 1 1 y+1
0 0 1 1 1 0 x-1
1 1 0 0 1 0 y-1
0 0 0 0 1 0 x+y
0 1 0 0 1 1 x-y
0 0 0 1 1 1 y-x
0 0 0 0 0 0 x&y
0 1 0 1 0 1 x|y

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 78


The Hack ALU operation: Compute x|y
pre-setting pre-setting selecting between post-setting Resulting
the x input the y input computing + or & the output ALU output
zx nx zy ny f no out

if zx if nx if zy if ny if f if no
then then then then then out=x+y then
x=0 x=!x y=0 y=!y else out=x&y out=!out out(x,y)=
1 0 1 0 1 0 0
1 1 1 1 1 1 1
1 1 1 0 1 0 -1 Practice:
0 0 1
Example:
1
compute
0
x|y 0 x
1 1 0 x: 0 0 1 00 1 0 y See if you get
0 0 1 y: 1 0 0 10 1 1 !x
0 1 1 1 (bitwise Or)
1 1 0 0 0 1 !y
0 0 1 1 1 1 -x
1 1 0 0 1 1 -y
0 1 1 1 1 1 x+1
1 1 0 1 1 1 y+1
0 0 1 1 1 0 x-1
1 1 0 0 1 0 y-1
0 0 0 0 1 0 x+y
0 1 0 0 1 1 x-y
0 0 0 1 1 1 y-x
0 0 0 0 0 0 x&y
0 1 0 1 0 1 x|y

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 79


The Hack ALU operation: Compute x|y
pre-setting pre-setting selecting between post-setting Resulting
the x input the y input computing + or & the output ALU output
zx nx zy ny f no out

if zx if nx if zy if ny if f if no
then then then then then out=x+y then
x=0 x=!x y=0 y=!y else out=x&y out=!out out(x,y)=
1 0 1 0 1 0 0
1 1 1 1 1 1 1
1 1 1 0 1 0 -1 Practice:
0 0 1
Example:
1
compute0
x|y 0 x
1 1 0 x: 0 0 1 00 1 0 y See if you get
0 0 1 y: 1 0 0 10 1 1 !x
0 1 1 1 (bitwise Or)
1 1 0 0
Following 0
pre-setting: 1 !y
0 0 1 1 1 1 -x
x: 1 0 1 0
1 1 0 0 1 1 -y
y: 1 1 0 0
0 1 1 1 1 1 x+1
1 1 0 Computation
1 and1 post-setting:1 y+1
0 0 1 x&y: 1 1 0 01 0 0 x-1
1 1 0 0
!(x&y): 0 1 11 1 0 y-1
0 0 0 0 1 0 x+y
0 1 0 0 1 1 x-y
0 0 0 1 1 1 y-x
0 0 0 0 0 0 x&y
0 1 0 1 0 1 x|y

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 80


The Hack ALU operation
pre-setting pre-setting selecting between post-setting Resulting
the x input the y input computing + or & the output ALU output
zx nx zy ny f no out

if zx if nx if zy if ny if f if no
then then then then then out=x+y then
x=0 x=!x y=0 y=!y else out=x&y out=!out out(x,y)=
1 0 1 0 1 0 0
1 1 1 1 1 1 1
1 1 1 0 1 0 -1
0 0 1 1 0 0 x
1 1 0 0 0 0 y
0 0 1 1 0 1 !x
1 1 0 0 0 1 !y
0 0 1 1 1 1 -x
1 1 0 0 1 1 -y
0 1 1 1 1 1 x+1
1 1 0 1 1 1 y+1
0 0 1 1 1 0 x-1
1 1 0 0 1 0 y-1
0 0 0 0 1 0 x+y
0 1 0 0 1 1 x-y
0 0 0 1 1 1 y-x
0 0 0 0 0 0 x&y
0 1 0 1 0 1 x|y

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 81


The Hack ALU operation: Compute y-1
pre-setting pre-setting selecting between post-setting Resulting
the x input the y input computing + or & the output ALU output
zx nx zy ny f no out

if zx if nx if zy if ny if f if no
then then then then then out=x+y then
x=0 x=!x y=0 y=!y else out=x&y out=!out out(x,y)=
Example: compute y-1
1 0 1 0 1 0 0
x: 0 1 0 1 (irrelevant)
1 1 1 1 1 1
y:1 0 1 1 0 (6)
1 1 1 0 1 0 -1
0 0 1 1 0 0 x
1 1 0 0 0 0 y
0 0 1 1 0 1 !x
1 1 0 0 0 1 !y
0 0 1 1 1 1 -x
1 1 0 0 1 1 -y
0 1 1 1 1 1 x+1
1 1 0 1 1 1 y+1
0 0 1 1 1 0 x-1
1 1 0 0 1 0 y-1
0 0 0 0 1 0 x+y Practice:
0 1 0 0 1 1 x-y
0 0 0 1 1 1 y-x See if you get
0 0 0 0 0 0 x&y
0 1 0 1 (5)
0 1 0 1 0 1 x|y

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 82


The Hack ALU operation: Compute y-1
pre-setting pre-setting selecting between post-setting Resulting
the x input the y input computing + or & the output ALU output
zx nx zy ny f no out

if zx if nx if zy if ny if f if no
then then then then then out=x+y then
x=0 x=!x y=0 y=!y else out=x&y out=!out out(x,y)=
Example: compute y-1
1 0 1 0 1 0 0
x: 0 1 0 1 (irrelevant)
1 1 1 1 1 1
y:1 0 1 1 0 (6)
1 1 1 0 1 0 -1
0 0 1 1 0 0 Following
x pre-setting:
1 1 0 0 0 0 x:y 1 1 1 1
0 0 1 1 0 1 y:!x 0 1 1 0
1 1 0 0 0 1 !y
Computation and post-setting:
0 0 1 1 1 1 -x
1 1 0 0 1 1 x+y:
-y 0 1 0 1
0 1 1 1 1 1 x+y:
x+1 0 1 0 1 (5)
1 1 0 1 1 1 y+1
0 0 1 1 1 0 x-1
1 1 0 0 1 0 y-1
0 0 0 0 1 0 x+y Practice:
0 1 0 0 1 1 x-y
0 0 0 1 1 1 y-x See if you get
0 0 0 0 0 0 x&y
0 1 0 1 (5)
0 1 0 1 0 1 x|y

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 83


The Hack ALU operation

One more detail:

zx nx zy ny f no

x
16 bits
if (out == 0) then zr = 1, else zr = 0
ALU 16 bits
out
y
16 bits if (out < 0) then ng = 1, else ng = 0

zr ng

The zr and ng output bits will come into play when we’ll build the
complete CPU architecture, later in the course.

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 84


Chapter 2: Boolean Arithmetic

Theory Practice

• Representing numbers • Arithmetic Logic Unit (ALU)

• Binary numbers • Project 2: Chips

• Boolean arithmetic • Project 2: Guidelines

• Signed numbers

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 85


Project 2
Given: All the chips built in Project 1

Goal: Build the chips:

• HalfAdder

• FullAdder

• Add16
• Inc16

• ALU

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 86


Half Adder
a b sum carry
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1

HalfAdder.hdl

/** Computes the sum of two bits. */


CHIP HalfAdder { Implementation tip
IN a, b; Can be built from two
OUT sum, carry;
gates built in project 1.
PARTS:
// Put your code here:
}

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 87


Full Adder
a b c sum carry
0 0 0 0 0
0 0 1 1 0
0 1 0 1 0
0 1 1 0 1
1 0 0 1 0
1 0 1 0 1
1 1 0 0 1
1 1 1 1 1
FullAdder.hdl

/** Computes the sum of three bits. */


CHIP FullAdder { Implementation tip
IN a, b, c;
OUT sum, carry; Can be built from two
PARTS:
half-adders.
// Put your code here:
}

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 88


16-bit adder

Add16.hdl

/* Adds two 16-bit, two’s-complement values. • The bitwise additions are done in parallel
The most-significant carry bit is ignored. */ • The carry propagation is sequential
CHIP Add16 {
• Yet… it works fine, as is.
IN a[16], b[16];
How? Stay tuned for chapter 3.
OUT out[16];
PARTS:
// Put you code here:
Implementation note
} If you need to set a pin x to 0 (or 1) in HDL,
use: x = false (or x = true)

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 89


16-bit incrementor

Inc16.hdl

/** Outputs in + 1. */
CHIP Inc16 {
IN in[16]; Implementation:
OUT out[16];
Simple.
PARTS:
// Put you code here:
}

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 90


ALU
zx nx zy ny f no

x
16 bits
ALU 16 bits
out
y
16 bits

zr ng

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 91


ALU
zx nx zy ny f no

x
16 bits
ALU 16 bits
out
y
16 bits

zr ng
ALU.hdl

/** The ALU */

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 92


ALU
zx nx zy ny f no

x
16 bits
ALU 16 bits
out
y
16 bits

zr ng
ALU.hdl

/** The ALU */


// Manipulates the x and y inputs as follows:
// if (zx == 1) sets x = 0 // 16-bit true
// if (nx == 1) sets x = !x // 16-bit Not
// if (zy == 1) sets y = 0 // 16-bit true
// if (ny == 1) sets y = !y // 16-bit Not
// if (f == 1) sets out = x + y // 2's-complement addition
// if (f == 0) sets out = x & y // 16-bit And
// if (no == 1) sets out = !out // 16-bit Not
// if (out == 0) sets zr = 1 // 1-bit true
// if (out < 0) sets ng = 1 // 1-bit true
...
Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 93
ALU
zx nx zy ny f no
Implementation tips
We need logic for:
x
• Implementing “if bit == 0/1” conditions
16 bits
ALU out
• Setting a 16-bit value to 0000000000000000
y
16 bits
16 bits
• Setting a 16-bit value to 1111111111111111
• Negating a 16-bit value (bitwise)
• Computing Add and Or on two 16-bit values
zr ng
ALU.hdl

/** The ALU */


// Manipulates the x and y inputs as follows:
// if (zx == 1) sets x = 0 // 16-bit true
// if (nx == 1) sets x = !x // 16-bit Not
Implementation strategy
// if (zy == 1) sets y = 0 // 16-bit true • Start by building an ALU
// if (ny == 1) sets y = !y // 16-bit Not that computes out
// if (f == 1) sets out = x + y // 2's-complement addition • Next, extend it to also
// if (f == 0) sets out = x & y // 16-bit And compute zr and ng.
// if (no == 1) sets out = !out // 16-bit Not
// if (out == 0) sets zr = 1 // 1-bit true
// if (out < 0) sets ng = 1 // 1-bit true
...
Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 94
Relevant bus tips
Using multi-bit truth / false constants:
...
// Suppose that x, y, z are 8-bit bus-pins:

7 6 5 4 3 2 1 0
x:

y:

z:

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 95


Relevant bus tips
Using multi-bit truth / false constants:
...
// Suppose that x, y, z are 8-bit bus-pins:
chipPart(..., x = true, y = false, z[0..2] = true, z[6..7] = true);
...

7 6 5 4 3 2 1 0
x:

y:

z:

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 96


Relevant bus tips
Using multi-bit truth / false constants:
...
// Suppose that x, y, z are 8-bit bus-pins:
chipPart(..., x = true, y = false, z[0..2] = true, z[6..7] = true);
...

7 6 5 4 3 2 1 0
x: 1 1 1 1 1 1 1 1

y:

z:

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 97


Relevant bus tips
Using multi-bit truth / false constants:
...
// Suppose that x, y, z are 8-bit bus-pins:
chipPart(..., x = true, y = false, z[0..2] = true, z[6..7] = true);
...

7 6 5 4 3 2 1 0
x: 1 1 1 1 1 1 1 1

y: 0 0 0 0 0 0 0 0

z:

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 98


Relevant bus tips
Using multi-bit truth / false constants:
We can assign values to sub-buses
...
// Suppose that x, y, z are 8-bit bus-pins:
chipPart(..., x = true, y = false, z[0..2] = true, z[6..7] = true);
...

7 6 5 4 3 2 1 0
x: 1 1 1 1 1 1 1 1

y: 0 0 0 0 0 0 0 0

z: 1 1 0 0 0 1 1 1

Unassigned bits are set to 0

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 99


Relevant bus tips
Sub-bussing:
• We can assign n-bit values to sub-buses, for any n
• We can create n-bit bus pins, for any n

/* 16-bit adder */
CHIP Add16 {
IN a[16], b[16];
OUT out[16];
PARTS:
CHIP Foo {
...
IN x[8], y[8], z[16]
}
OUT out[16]
PARTS
...
Add16 (a[0..7] = x, a[8..15] = y, b = z, out = …);
...
Add16 (a = …, b = …, out[0..3] = t1, out[4..15] = t2);
...
}

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 100


Relevant bus tips
Sub-bussing:
• We can assign n-bit values to sub-buses, for any n
• We can create n-bit bus pins, for any n

/* 16-bit adder */
CHIP Add16 {
IN a[16], b[16];
OUT out[16];
PARTS:
CHIP Foo {
... Another example of assigning
IN x[8], y[8], z[16]
} a multi-bit value to a sub-bus
OUT out[16]
PARTS
...
Add16 (a[0..7] = x, a[8..15] = y, b = z, out = …);
...
Add16 (a = …, b = …, out[0..3] = t1, out[4..15] = t2);
...
}

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 101


Relevant bus tips
Sub-bussing:
• We can assign n-bit values to sub-buses, for any n
• We can create n-bit bus pins, for any n

/* 16-bit adder */
CHIP Add16 {
IN a[16], b[16];
OUT out[16];
PARTS:
CHIP Foo {
... Another example of assigning
IN x[8], y[8], z[16]
} a multi-bit value to a sub-bus
OUT out[16]
PARTS
...
Add16 (a[0..7] = x, a[8..15] = y, b = z, out = …);
...
Add16 (a = …, b = …, out[0..3] = t1, out[4..15] = t2);
...
}
Creating an n-bit bus (internal pin)

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 102


ALU: Recap
zx nx zy ny f no

x
16 bits
ALU 16 bits
out
y
16 bits

zr ng

The Hack ALU is: “Simplicity is the


ultimate sophistication.”
• Simple
― Leonardo da Vinci
• Elegant

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 103


Chapter 2: Boolean Arithmetic

Theory Practice

• Representing numbers • Arithmetic Logic Unit (ALU)

• Binary numbers • Project 2: Chips

• Boolean arithmetic • Project 2: Guidelines

• Signed numbers

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 104


Project 2
Given: All the chips built in Project 1

Goal: Build the chips:


• HalfAdder
• FullAdder
• Add16
• Inc16
• ALU

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 105


Guidelines: www.nand2tetris.org/project02

www.nand2tetris.org

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 106


Resources
Project 2 folder (.hdl, .tst, .cmp files): nand2tetris/projects/02

Tools
• Text editor (for completing the given .hdl stub-files)
• Hardware simulator: nand2tetris/tools

Guides
• Hardware Simulator Tutorial
• HDL Guide
• Hack Chip Set API

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 107


Chip interfaces: Hack chip set API

Open the API in a window, and copy-paste


Add16 (a= ,b= ,out= );
chip signatures into your HDL code, as needed
ALU (x= ,y= ,zx= ,nx= ,zy= ,ny= ,f= ,no= ,out= ,zr= ,ng= );
And16 (a= ,b= ,out= );
And (a= ,b= ,out= ); Mux8Way (a= ,b= ,c= ,d= ,e= ,f= ,g= ,h= ,sel= ,out= );
Aregister (in= ,load= ,out= ); Mux (a= ,b= ,sel= ,out= );
Bit (in= ,load= ,out= ); Nand (a= ,b= ,out= );
Not16,pc=
CPU (inM= ,instruction= ,reset= ,outM= ,writeM= ,addressM= (in=);,out= );
DFF (in= ,out= ); Not (in= ,out= );
DMux4Way (in= ,sel= ,a= ,b= ,c= ,d= ); Or16 (a= ,b= ,out= );
DMux8Way (in= ,sel= ,a= ,b= ,c= ,d= ,e= ,f= ,g= ,h= ); Or8Way (in= ,out= );
Dmux (in= ,sel= ,a= ,b= ); Or (a= ,b= ,out= );
Dregister (in= ,load= ,out= ); PC (in= ,load= ,inc= ,reset= ,out= );
FullAdder (a= ,b= ,c= ,sum= ,carry= ); PCLoadLogic (cinstr= ,j1= ,j2= ,j3= ,load= ,inc= );
HalfAdder (a= ,b= ,sum= , carry= ); RAM16K (in= ,load= ,address= ,out= );
Inc16 (in= ,out= ); RAM4K (in= ,load= ,address= ,out= );
Keyboard (out= ); RAM512 (in= ,load= ,address= ,out= );
Memory (in= ,load= ,address= ,out= ); RAM64 (in= ,load= ,address= ,out= );
Mux16 (a= ,b= ,sel= ,out= ); RAM8 (in= ,load= ,address= ,out= );
Mux4Way16 (a= ,b= ,c= ,d= ,sel= ,out= ); Register (in= ,load= ,out= );
ROM32K (address= ,out= );
Mux8Way16 (a= ,b= ,c= ,d= ,e= ,f= ,g= ,h= ,sel= ,out= );
Screen (in= ,load= ,address= ,out= );
Xor (a= ,b= ,out= );

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 108


Best practice advice (same as project 1)
• Implement the chips in the order in which they appear in the project guidelines
• If you don’t implement some chips, you can still use their built-in implementations
• No need for “helper chips”: Implement / use only the chips we specified
• In each chip definition, strive to use as few chip-parts as possible

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 109


Best practice advice
• Implement the chips in the order in which they appear in the project guidelines
• If you don’t implement some chips, you can still use their built-in implementations
• No need for “helper chips”: Implement / use only the chips we specified
• In each chip definition, strive to use as few chip-parts as possible
• You will have to use chips implemented in Project 1;
• For efficiency and consistency’s sake, use their built-in versions, rather than your
own HDL implementations.

That’s It!
Go Do Project 2!

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 110


Chapter 2: Boolean Arithmetic

Theory Practice

• Representing numbers • Arithmetic Logic Unit (ALU)

• Binary numbers • Project 2: Chips

• Boolean arithmetic • Project 2: Guidelines

• Representing signed numbers

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 111


What’s next?
abstraction
machine
language
This lecture / chapter / project:
assembler p6 Build the computer’s ALU

Building a
hardware platform
abstraction
computer
abstraction Building
computer
p4 chips
ALU, RAM abstraction Building
p5 p2 gates
elementary
logic gates
p3 p1

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 112


What’s next?
abstraction
machine
language
This lecture / chapter / project:
assembler p6 Build the computer’s ALU

Building a
hardware platform
abstraction
computer
abstraction Building
computer
p4 chips
ALU, RAM abstraction Building
p5 p2 gates
elementary
logic gates
p3 p1

Next lecture / chapter / project:


Build the computer’s RAM

Nand to Tetris / www.nand2tetris.org / Chapter 2 Slide 113

You might also like