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
Counting systems
quantity decimal binary 3quantity decimal binary 3quantity decimal binary 3quantity decimal binary 3----bit registerbit registerbit registerbit 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 3
Rationale
192121202021)10011( 01234
=⋅+⋅+⋅+⋅+⋅=two
i
n
i
ibnn bxxxx ⋅= ∑=
−
0
01 )...(
9038018013010019)9038( 0123
=⋅+⋅+⋅+⋅=ten
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 4
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 5
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 6
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 7
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 8
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
fu ll
ad d er
a
sum
b
carry
c
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 9
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 10
The ALU (of the Hack platform)
half
adder
a sum
b carry
full
ad der
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 11
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 12
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 13
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 14
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

PDF
Boolean arithmetic
PDF
Introduction
PDF
Lecture 12 os
PDF
nand2tetris 舊版投影片 -- 第五章 計算機結構
PDF
nand2tetris 舊版投影片 -- 第一章 布林邏輯
PDF
Sequential logic
PDF
Computer architecture
PDF
nand2tetris 舊版投影片 -- 第三章 循序邏輯
Boolean arithmetic
Introduction
Lecture 12 os
nand2tetris 舊版投影片 -- 第五章 計算機結構
nand2tetris 舊版投影片 -- 第一章 布林邏輯
Sequential logic
Computer architecture
nand2tetris 舊版投影片 -- 第三章 循序邏輯

What's hot (20)

PDF
Lecture 11 compiler ii
PDF
Lecture 09 high level language
PDF
Lecture 08 virtual machine ii
PDF
Lecture 07 virtual machine i
PDF
Lecture 10 compiler i
PDF
nand2tetris 舊版投影片 -- 第四章 機器語言
PDF
Lecture 06 assembler
PDF
Machine language
PPTX
PDF
Matlab workshop
PPT
Computer organiztion2
PDF
Redo midterm
PDF
Paper Ball
PPTX
tic-tac-toe: Game playing
PPTX
Tic Tac Toe using Mini Max Algorithm
PPT
Unit 2 module-2
PPTX
Sequential and combinational alu
PPT
Ceng232 Decoder Multiplexer Adder
PPTX
System design using HDL - Module 3
PDF
FLASHBRAIN report
Lecture 11 compiler ii
Lecture 09 high level language
Lecture 08 virtual machine ii
Lecture 07 virtual machine i
Lecture 10 compiler i
nand2tetris 舊版投影片 -- 第四章 機器語言
Lecture 06 assembler
Machine language
Matlab workshop
Computer organiztion2
Redo midterm
Paper Ball
tic-tac-toe: Game playing
Tic Tac Toe using Mini Max Algorithm
Unit 2 module-2
Sequential and combinational alu
Ceng232 Decoder Multiplexer Adder
System design using HDL - Module 3
FLASHBRAIN report
Ad

Similar to nand2tetris 舊版投影片 -- 第二章 布林算術 (20)

PPT
lecture 02 Boolean arithmetic.ppt
PDF
Boolean arithmetic/nandtotetris/lecture_2
PDF
Nand2 tetris 1and2
PPTX
.Comp Org and Arch All_Slide_1711695396000.pptx
PPT
An Introduction to Computer Logic and Digital Design.ppt
PPT
Data Structure and AlgorithmChapter1.ppt
PPT
computer system architecture for control system.ppt
PPTX
EE5440 – Computer Architecture - Lecture 1
PPSX
Coa presentation1
PDF
Introduction to Computing Systems: From Bits & Gates to C & Beyond 3rd Editio...
PPT
Chapter1.ppt
PPT
computer logic and digital design chapter 1
PPTX
ICT FIRST LECTURE.pptx
PPT
Chapter1
PPT
f37-book-intarch-pres-pt1.ppt
PPT
f37-book-intarch-pres-pt1.ppt
PPT
04-logic-gates.ppt
PPT
04-logic-gates (1).ppt
PPT
CA.ppt
PPT
basic_gates.ppt
lecture 02 Boolean arithmetic.ppt
Boolean arithmetic/nandtotetris/lecture_2
Nand2 tetris 1and2
.Comp Org and Arch All_Slide_1711695396000.pptx
An Introduction to Computer Logic and Digital Design.ppt
Data Structure and AlgorithmChapter1.ppt
computer system architecture for control system.ppt
EE5440 – Computer Architecture - Lecture 1
Coa presentation1
Introduction to Computing Systems: From Bits & Gates to C & Beyond 3rd Editio...
Chapter1.ppt
computer logic and digital design chapter 1
ICT FIRST LECTURE.pptx
Chapter1
f37-book-intarch-pres-pt1.ppt
f37-book-intarch-pres-pt1.ppt
04-logic-gates.ppt
04-logic-gates (1).ppt
CA.ppt
basic_gates.ppt
Ad

More from 鍾誠 陳鍾誠 (20)

PDF
用十分鐘瞭解 新竹科學園區的發展史
PDF
用十分鐘搞懂 λ-Calculus
PDF
交⼤資訊⼯程學系備審資料 ⾱詠祥
PDF
smallpt: Global Illumination in 99 lines of C++
PDF
西洋史 (你或許不知道但卻影響現代教育的那些事)
PDF
區塊鏈 (比特幣背後的關鍵技術) -- 十分鐘系列
PDF
區塊鏈 (比特幣背後的關鍵技術) -- 十分鐘系列
PDF
梯度下降法 (隱藏在深度學習背後的演算法) -- 十分鐘系列
PDF
用十分鐘理解 《微分方程》
DOCX
系統程式 -- 前言
DOCX
系統程式 -- 附錄
DOCX
系統程式 -- 第 12 章 系統軟體實作
DOCX
系統程式 -- 第 11 章 嵌入式系統
DOCX
系統程式 -- 第 10 章 作業系統
DOCX
系統程式 -- 第 9 章 虛擬機器
DOCX
系統程式 -- 第 8 章 編譯器
DOCX
系統程式 -- 第 7 章 高階語言
DOCX
系統程式 -- 第 6 章 巨集處理器
DOCX
系統程式 -- 第 5 章 連結與載入
DOCX
系統程式 -- 第 4 章 組譯器
用十分鐘瞭解 新竹科學園區的發展史
用十分鐘搞懂 λ-Calculus
交⼤資訊⼯程學系備審資料 ⾱詠祥
smallpt: Global Illumination in 99 lines of C++
西洋史 (你或許不知道但卻影響現代教育的那些事)
區塊鏈 (比特幣背後的關鍵技術) -- 十分鐘系列
區塊鏈 (比特幣背後的關鍵技術) -- 十分鐘系列
梯度下降法 (隱藏在深度學習背後的演算法) -- 十分鐘系列
用十分鐘理解 《微分方程》
系統程式 -- 前言
系統程式 -- 附錄
系統程式 -- 第 12 章 系統軟體實作
系統程式 -- 第 11 章 嵌入式系統
系統程式 -- 第 10 章 作業系統
系統程式 -- 第 9 章 虛擬機器
系統程式 -- 第 8 章 編譯器
系統程式 -- 第 7 章 高階語言
系統程式 -- 第 6 章 巨集處理器
系統程式 -- 第 5 章 連結與載入
系統程式 -- 第 4 章 組譯器

Recently uploaded (20)

PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PPTX
Education and Perspectives of Education.pptx
PDF
MICROENCAPSULATION_NDDS_BPHARMACY__SEM VII_PCI .pdf
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PDF
My India Quiz Book_20210205121199924.pdf
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PDF
advance database management system book.pdf
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
Journal of Dental Science - UDMY (2021).pdf
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PPTX
Core Concepts of Personalized Learning and Virtual Learning Environments
PDF
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
PDF
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PPTX
Introduction to pro and eukaryotes and differences.pptx
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
Cambridge-Practice-Tests-for-IELTS-12.docx
Education and Perspectives of Education.pptx
MICROENCAPSULATION_NDDS_BPHARMACY__SEM VII_PCI .pdf
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
My India Quiz Book_20210205121199924.pdf
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
advance database management system book.pdf
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
FORM 1 BIOLOGY MIND MAPS and their schemes
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
What if we spent less time fighting change, and more time building what’s rig...
Journal of Dental Science - UDMY (2021).pdf
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
Core Concepts of Personalized Learning and Virtual Learning Environments
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
Introduction to pro and eukaryotes and differences.pptx

nand2tetris 舊版投影片 -- 第二章 布林算術

  • 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 Counting systems quantity decimal binary 3quantity decimal binary 3quantity decimal binary 3quantity decimal binary 3----bit registerbit registerbit registerbit 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
  • 3. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 3 Rationale 192121202021)10011( 01234 =⋅+⋅+⋅+⋅+⋅=two i n i ibnn bxxxx ⋅= ∑= − 0 01 )...( 9038018013010019)9038( 0123 =⋅+⋅+⋅+⋅=ten
  • 4. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 4 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 ++++
  • 5. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 5 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
  • 6. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 6 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
  • 7. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 7 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
  • 8. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 8 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 fu ll ad d er a sum b carry c
  • 9. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 9 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 ++++
  • 10. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 10 The ALU (of the Hack platform) half adder a sum b carry full ad der 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
  • 11. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 11 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.
  • 12. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 12 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)
  • 13. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 13 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.
  • 14. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 2: Boolean Arithmetic slide 14 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