CS 201 Lecture 3
CS 201 Lecture 3
Lecture 3
Boolean Arithmetic
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
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
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
CPU
ALU
Input Memory Output
Registers
ALU functions ( f )
• Arithmetic: x + y, x – y, x + 1, x – 1, ...
• Logical: x & y, x | y, !x , ...
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.
Theory Practice
• Signed numbers
Theory Practice
• Signed numbers
17
This is not seventeen.
Twenty seven
goats
Unary:
Egyptian:
Roman: XXVII
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)
Six thousands,
... five hundreds,
and seven goats
3 2 1 0
6507
𝑛−1
Theory Practice
Seven thousands
... and fifty three
goats
3 2 1 0
7 0 5 3 10
𝑛−1
Seven thousands
... and fifty three
goats
𝑛−1
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
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
Inside computers,
everything is binary
G.W. Leibnitz
(1646 – 1716)
Worshipped
binary numbers
Theory Practice
• Signed numbers
• Subtraction
We’ll get it for free
• Multiplication
Based on addition
• Division
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
0 0 1 0
1 0 1 0
+
1 1
1 1 0 1
Binary addition
0 0 1 0
1 0 1 0 0 0 0 1
+ +
0 0 1 1 0 1 0 1
1 1 0 1
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
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
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
Handling overflow
• Our decision: Ignore it
• As we will soon see, ignoring the overflow bit is not a bug, it’s a feature.
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.
Theory Practice
• Signed numbers
Theory Practice
• Signed numbers
(x + y, –x + y, x + –y, –x + –y)
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.
Theory Practice
• Signed numbers
Theory Practice
• Signed numbers
CPU
ALU
Input Memory Output
Registers
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, …)
zx nx zy ny f no
x
16 bits
ALU 16 bits
out
y
16 bits
zr ng
Built-in ALU
implementation
Set to binary
I/O format
Inspect the
ALU outputs
zx nx zy ny f no
x
16 bits
ALU 16 bits
out
y
16 bits
zr ng
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
Theory Practice
• Signed numbers
• HalfAdder
• FullAdder
• Add16
• Inc16
• ALU
HalfAdder.hdl
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)
Inc16.hdl
/** Outputs in + 1. */
CHIP Inc16 {
IN in[16]; Implementation:
OUT out[16];
Simple.
PARTS:
// Put you code here:
}
x
16 bits
ALU 16 bits
out
y
16 bits
zr ng
x
16 bits
ALU 16 bits
out
y
16 bits
zr ng
ALU.hdl
x
16 bits
ALU 16 bits
out
y
16 bits
zr ng
ALU.hdl
7 6 5 4 3 2 1 0
x:
y:
z:
7 6 5 4 3 2 1 0
x:
y:
z:
7 6 5 4 3 2 1 0
x: 1 1 1 1 1 1 1 1
y:
z:
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:
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
/* 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);
...
}
/* 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);
...
}
/* 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)
x
16 bits
ALU 16 bits
out
y
16 bits
zr ng
Theory Practice
• Signed numbers
www.nand2tetris.org
Tools
• Text editor (for completing the given .hdl stub-files)
• Hardware simulator: nand2tetris/tools
Guides
• Hardware Simulator Tutorial
• HDL Guide
• Hack Chip Set API
That’s It!
Go Do Project 2!
Theory Practice
Building a
hardware platform
abstraction
computer
abstraction Building
computer
p4 chips
ALU, RAM abstraction Building
p5 p2 gates
elementary
logic gates
p3 p1
Building a
hardware platform
abstraction
computer
abstraction Building
computer
p4 chips
ALU, RAM abstraction Building
p5 p2 gates
elementary
logic gates
p3 p1