SlideShare a Scribd company logo
1 of 15
Download to read offline
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 1
www.nand2tetris.org
Building a Modern Computer From First Principles
Boolean Arithmetic
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 2
Usage and Copyright Notice:
Copyright © Noam Nisan and Shimon Schocken
This presentation contains lecture materials that accompany the textbook “The Elements of
Computing Systems” by Noam Nisan & Shimon Schocken, MIT Press, 2005.
We provide both PPT and PDF versions.
Our web site, www.nand2tetris.org ,features a set of presentations, one for each book chapter.
Each presentation is designed to support about 3 hours of classroom or self-study instruction.
You are welcome to use or edit this presentation as you see fit for instructional and non-
commercial purposes.
If you use our materials, please include a reference to www.nand2tetris.org
If you have any questions or comments, please write us at nand2tetris@gmail.com
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 3
Counting systems
quantity decimal binary 3-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 4
Rationale
19
2
1
2
1
2
0
2
0
2
1
)
10011
( 0
1
2
3
4











two
i
n
i
i
b
n
n b
x
x
x
x 
 


0
0
1 )
...
(
9038
0
1
8
0
1
3
0
1
0
0
1
9
)
9038
( 0
1
2
3









ten
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 5
no overflow overflow
 Algorithm: exactly the same as in decimal addition
 Overflow (MSB carry) has to be dealt with.
Binary addition
Assuming a 4-bit system:
0 0 0 1
1 0 0 1
0 1 0 1
0 1 1 1 0
+
1 1 1 1
1 0 1 1
0 1 1 1
1 0 0 1 0
+
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 6
Representing negative numbers (4-bit system)
 The codes of all positive numbers
begin with a “0”
 The codes of all negative numbers
begin with a “1“
 To convert a number:
leave all trailing 0’s and first 1 intact,
and flip all the remaining bits
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
Example: 2 - 5 = 2 + (-5) = 0 0 1 0
+ 1 0 1 1
1 1 0 1 = -3
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 7
Building an Adder chip
 Adder: a chip designed to add two integers
 Proposed implementation:
 Half adder: designed to add 2 bits
 Full adder: designed to add 3 bits
 Adder: designed to add two n-bit numbers.
out
a
16
16-bit
adder
b
16
16
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 8
Half adder (designed to add 2 bits)
Implementation: based on two gates that you’ve seen before.
half
adder
a sum
b carry
a b sum carry
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 9
Full adder (designed to add 3 bits)
Implementation: can be based on half-adder gates.
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
full
adder
a
sum
b
carry
c
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 10
n-bit Adder (designed to add two 16-bit numbers)
Implementation: array of full-adder gates.
out
a
16
16-bit
adder
b
16
16
... 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 11
The ALU (of the Hack platform)
half
adder
a sum
b carry
full
adder
a
sum
b
carry
c
out
x
16
16-bit
adder
y
16
16
zx no
zr
nx zy ny f
ALU
ng
16 bits
16 bits
x
y 16 bits
out
out(x, y, control bits) =
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
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 12
ALU logic (Hack platform)
Implementation: build a logic gate architecture
that “executes” the control bit “instructions”:
if zx==1 then set x to 0 (bit-wise), etc.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 13
The ALU in the CPU context (a sneak preview of the Hack platform)
ALU
Mux
D
out
A/M
a
D register
A register
A
M
c1,c2,… ,c6
RAM
(selected
register)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 14
Perspective
 Combinational logic
 Our adder design is very basic: no parallelism
 It pays to optimize adders
 Our ALU is also very basic: no multiplication, no division
 Where is the seat of more advanced math operations?
a typical hardware/software tradeoff.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 15
Historical end-note: Leibnitz (1646-1716)
 “The binary system may be used in place of the decimal system;
express all numbers by unity and by nothing”
 1679: built a mechanical calculator (+, -, *, /)
 CHALLENGE: “All who are occupied with the
reading or writing of scientific literature have
assuredly very often felt the want of a common
scientific language, and regretted the great loss of
time and trouble caused by the multiplicity of
languages employed in scientific literature:
 SOLUTION: “Characteristica Universalis”: a
universal, formal, and decidable language of
reasoning
 The dream’s end: Turing and Gödel in 1930’s.
Leibniz’s medallion
for the Duke of Brunswick

More Related Content

Similar to lecture 02 Boolean arithmetic.ppt

Computer organiztion1
Computer organiztion1Computer organiztion1
Computer organiztion1
Umang Gupta
 
File 1 proteus tutorial for digital circuit design
File 1 proteus tutorial for digital circuit designFile 1 proteus tutorial for digital circuit design
File 1 proteus tutorial for digital circuit design
Sanjeev Singh
 
Magical float repr
Magical float reprMagical float repr
Magical float repr
dickinsm
 
04 Unit IV DTE.pptx
04 Unit IV DTE.pptx04 Unit IV DTE.pptx
04 Unit IV DTE.pptx
Harsheye
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OS
C.U
 

Similar to lecture 02 Boolean arithmetic.ppt (20)

ALU.ppt
ALU.pptALU.ppt
ALU.ppt
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Unit 2 module-2
Unit 2 module-2Unit 2 module-2
Unit 2 module-2
 
Computer organiztion1
Computer organiztion1Computer organiztion1
Computer organiztion1
 
Class 26: Objectifying Objects
Class 26: Objectifying ObjectsClass 26: Objectifying Objects
Class 26: Objectifying Objects
 
nand2tetris 舊版投影片 -- 第五章 計算機結構
nand2tetris 舊版投影片 -- 第五章 計算機結構nand2tetris 舊版投影片 -- 第五章 計算機結構
nand2tetris 舊版投影片 -- 第五章 計算機結構
 
Lecture 07 virtual machine i
Lecture 07 virtual machine iLecture 07 virtual machine i
Lecture 07 virtual machine i
 
Lecture 11 compiler ii
Lecture 11 compiler iiLecture 11 compiler ii
Lecture 11 compiler ii
 
Lab1
Lab1Lab1
Lab1
 
File 1 proteus tutorial for digital circuit design
File 1 proteus tutorial for digital circuit designFile 1 proteus tutorial for digital circuit design
File 1 proteus tutorial for digital circuit design
 
COMPUTER ORGNIZATION AND ASSEMBLY LANGUAGE EBOOK
COMPUTER ORGNIZATION AND ASSEMBLY LANGUAGE EBOOKCOMPUTER ORGNIZATION AND ASSEMBLY LANGUAGE EBOOK
COMPUTER ORGNIZATION AND ASSEMBLY LANGUAGE EBOOK
 
[01] Exit Exam Preparation 2023 DLD.pptx
[01] Exit Exam Preparation 2023 DLD.pptx[01] Exit Exam Preparation 2023 DLD.pptx
[01] Exit Exam Preparation 2023 DLD.pptx
 
Simulink
SimulinkSimulink
Simulink
 
Lecture 09 high level language
Lecture 09 high level languageLecture 09 high level language
Lecture 09 high level language
 
Magical float repr
Magical float reprMagical float repr
Magical float repr
 
Lecture 2 - Bit vs Qubits.pptx
Lecture 2 - Bit vs Qubits.pptxLecture 2 - Bit vs Qubits.pptx
Lecture 2 - Bit vs Qubits.pptx
 
Probabilistic data structures. Part 3. Frequency
Probabilistic data structures. Part 3. FrequencyProbabilistic data structures. Part 3. Frequency
Probabilistic data structures. Part 3. Frequency
 
04 Unit IV DTE.pptx
04 Unit IV DTE.pptx04 Unit IV DTE.pptx
04 Unit IV DTE.pptx
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OS
 
Lecture 12 os
Lecture 12 osLecture 12 os
Lecture 12 os
 

Recently uploaded

SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 

Recently uploaded (20)

FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 

lecture 02 Boolean arithmetic.ppt

  • 1. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 1 www.nand2tetris.org Building a Modern Computer From First Principles Boolean Arithmetic
  • 2. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 2 Usage and Copyright Notice: Copyright © Noam Nisan and Shimon Schocken This presentation contains lecture materials that accompany the textbook “The Elements of Computing Systems” by Noam Nisan & Shimon Schocken, MIT Press, 2005. We provide both PPT and PDF versions. Our web site, www.nand2tetris.org ,features a set of presentations, one for each book chapter. Each presentation is designed to support about 3 hours of classroom or self-study instruction. You are welcome to use or edit this presentation as you see fit for instructional and non- commercial purposes. If you use our materials, please include a reference to www.nand2tetris.org If you have any questions or comments, please write us at nand2tetris@gmail.com
  • 3. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 3 Counting systems quantity decimal binary 3-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
  • 4. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 4 Rationale 19 2 1 2 1 2 0 2 0 2 1 ) 10011 ( 0 1 2 3 4            two i n i i b n n b x x x x      0 0 1 ) ... ( 9038 0 1 8 0 1 3 0 1 0 0 1 9 ) 9038 ( 0 1 2 3          ten
  • 5. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 5 no overflow overflow  Algorithm: exactly the same as in decimal addition  Overflow (MSB carry) has to be dealt with. Binary addition Assuming a 4-bit system: 0 0 0 1 1 0 0 1 0 1 0 1 0 1 1 1 0 + 1 1 1 1 1 0 1 1 0 1 1 1 1 0 0 1 0 +
  • 6. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 6 Representing negative numbers (4-bit system)  The codes of all positive numbers begin with a “0”  The codes of all negative numbers begin with a “1“  To convert a number: leave all trailing 0’s and first 1 intact, and flip all the remaining bits 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 Example: 2 - 5 = 2 + (-5) = 0 0 1 0 + 1 0 1 1 1 1 0 1 = -3
  • 7. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 7 Building an Adder chip  Adder: a chip designed to add two integers  Proposed implementation:  Half adder: designed to add 2 bits  Full adder: designed to add 3 bits  Adder: designed to add two n-bit numbers. out a 16 16-bit adder b 16 16
  • 8. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 8 Half adder (designed to add 2 bits) Implementation: based on two gates that you’ve seen before. half adder a sum b carry a b sum carry 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 1
  • 9. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 9 Full adder (designed to add 3 bits) Implementation: can be based on half-adder gates. 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 full adder a sum b carry c
  • 10. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 10 n-bit Adder (designed to add two 16-bit numbers) Implementation: array of full-adder gates. out a 16 16-bit adder b 16 16 ... 1 0 1 1 a … 0 0 1 0 b … 1 1 0 1 out +
  • 11. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 11 The ALU (of the Hack platform) half adder a sum b carry full adder a sum b carry c out x 16 16-bit adder y 16 16 zx no zr nx zy ny f ALU ng 16 bits 16 bits x y 16 bits out out(x, y, control bits) = 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
  • 12. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 12 ALU logic (Hack platform) Implementation: build a logic gate architecture that “executes” the control bit “instructions”: if zx==1 then set x to 0 (bit-wise), etc.
  • 13. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 13 The ALU in the CPU context (a sneak preview of the Hack platform) ALU Mux D out A/M a D register A register A M c1,c2,… ,c6 RAM (selected register)
  • 14. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 14 Perspective  Combinational logic  Our adder design is very basic: no parallelism  It pays to optimize adders  Our ALU is also very basic: no multiplication, no division  Where is the seat of more advanced math operations? a typical hardware/software tradeoff.
  • 15. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 15 Historical end-note: Leibnitz (1646-1716)  “The binary system may be used in place of the decimal system; express all numbers by unity and by nothing”  1679: built a mechanical calculator (+, -, *, /)  CHALLENGE: “All who are occupied with the reading or writing of scientific literature have assuredly very often felt the want of a common scientific language, and regretted the great loss of time and trouble caused by the multiplicity of languages employed in scientific literature:  SOLUTION: “Characteristica Universalis”: a universal, formal, and decidable language of reasoning  The dream’s end: Turing and Gödel in 1930’s. Leibniz’s medallion for the Duke of Brunswick