lecture 02 Boolean arithmetic

Building a Modern Computer From First Principles
www.nand2tetris.org

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic

slide 1

Counting systems
quantity

decimal

binary

33-bit register

0

0

000


1

1

001

2

10

010

3

11

011

4


100

100

5

101

101

6

110

110

7

111


111

8

1000

overflow

9

1001

overflow

10

1010

overflow


Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic

slide 2

Rationale

(9038) ten = 9 ⋅103 + 0 ⋅10 2 + 3 ⋅101 + 8 ⋅10 0 = 9038

(10011) two = 1 ⋅ 2 4 + 0 ⋅ 2 3 + 0 ⋅ 2 2 + 1 ⋅ 21 + 1 ⋅ 2 0 = 19

n

( xn xn−1 ...x0 ) b = ∑ xi ⋅ b

i

i =0

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic


slide 3

Binary addition

!

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

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

0 1 1 1 0

1 0 0 1 0

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic


slide 4

Representing negative numbers (4-bit system)
"

0

0000

1

0001

1111

-1

2


0010

1110

-2

3

0011

1101

-3

4

0100

1100


-4

5

0101

1011

-5

6

0110

1010

-6

7


0111

1001

-7

1000

-8

*

#

#
$%&

"
$'$
"


2 - 5 = 2 + (-5) =

%(

'

)

#

0010
+1011
1101

= -3

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic

slide 5


Building an Adder chip

16

a
16

b

16

1 6 -b it
ad d e r

o ut

#
+

#

#

,

-

.

/
!

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic

slide 6

Half adder (designed to add 2 bits)

0 #

sum carry

a

b

0

0

0

0

0

1

1

0

1

0

1

0

1

1

0

1

a
b

h alf
ad d er

s um
c arry

(

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic

slide 7

Full adder (designed to add 3 bits)

0 #

sum carry

a

b

c

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

a
s um

fu ll
ad d er

b

c arry

c

!

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic

slide 8

n-bit Adder (designed to add two 16-bit numbers)
16

a
16

b

0 #

16

1 6 -b it
ad d e r

o ut

...

1

0

1

1

a



0

0

1

0

b



1

1

0

1

out

+

!

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic

slide 9

The ALU (of the Hack platform)

a
b

s um

h alf
ad d er

16

a
s um

fu ll
ad d e r

b

c arry

16

c arry

c

zx nx

zy

ny

f

x

no

y

16-bit
adder

out(x, y,

16

out

) =

x+y, x-y, y–x,
0, 1, -1,

x
16 bits

ALU

y

out

x, y, -x, -y,

16 bits

x!, y!,

16 bits

x+1, y+1, x-1, y-1,
zr

ng

x&y, x|y

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic

slide 10

ALU logic (Hack platform)

0 #
$
1 22'

&

$
%

!

&
)

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic

slide 11

The ALU in the CPU context (a sneak preview of the Hack platform)
c1,c2, … ,c6

D
D register

a
ALU

A register

A
Mux

RAM

out

A/M

M

(selected
register)

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic

slide 12

Perspective
3
#
0 #

#

1

45

#

6

)
#

#

7

8

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic

slide 13

Historical end-note: Leibnitz (1646-1716)
$"

#

9

#

&

':;<

=) !) >) 8

3, 44*?@* $

#

)
#
#
45"0 ? $3
)
)
"

(

5

"

@A

&

Leibniz’s medallion
for the Duke of Brunswick

'