lecture 2 (2).pdf
Document Details
Uploaded by Deleted User
2021
Tags
Full Transcript
From Nand to Tetris building a Modern Computer from First Principles Lecture 2 Boolean Arithmetic These slides support chapter 2 of the book The Elements of Comput...
From Nand to Tetris building a Modern Computer from First Principles Lecture 2 Boolean Arithmetic These slides support chapter 2 of the book The Elements of Computing Systems By Noam Nisan and Shimon Schocken MIT Press, 2021 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 1 Nand to Tetris Roadmap: Hardware abstraction machine language assembler p6 abstraction p4 p5 hardware platform building a p2 p3 computer abstraction computer building p1 ALU, RAM abstraction chips building elementary d logic gates gates Nan Project 1 Build 15 elementary logic gates Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 2 Nand to Tetris Roadmap: Hardware abstraction machine language assembler p6 abstraction p4 p5 hardware platform building a p2 p3 computer abstraction computer building p1 ALU, RAM abstraction chips building elementary d logic gates gates Nan Project 2 Build chips that do arithmetic, leading up to an ALU Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 3 Computer system CPU ALU Input Memory Output Registers Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 4 Computer system CPU ALU Input Memory Output Registers Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 5 Arithmetic Logical Unit (40521) + 1001111001001001 (40538) 1001111001011010 Computes a given function on (17) ALU two n-bit input 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, !x,... Challenges Use 0’s and 1’s for representing numbers Use logic gates for realizing arithmetic / logical functions. Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 6 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 / Copyright © Noam Nisan and Shimon Schocken Slide 7 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 / Copyright © Noam Nisan and Shimon Schocken Slide 8 Representation This is not a pipe (by René Magritte) Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 9 Representation 17 This is not seventeen. It’s an agreed-upon code (numeral) that represents the number seventeen. Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 10 A brief history of numeral systems Twenty seven goats Unary: Egyptian: Roman: XXVII Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 11 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 1,000 years ago Hindered the progress of Algebra (and commerce, science, technology) Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 12 Positional numeral system Six thousands,... five hundreds, and seven goats 3 2 1 0 6507 "#$ ! 𝑑% # 10% = 6 # 10& + 5 # 10' + 0 # 10$ + 7 # 10! = 6507 ! 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 at Note: The method mentions position i Base: The number of symbols no specific base. Numeral: An ordered sequence of digits Value: The digit at 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 / Copyright © Noam Nisan and Shimon Schocken Slide 13 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 / Copyright © Noam Nisan and Shimon Schocken Slide 14 Positional number system Seven thousands... and fifty three goats Decimal (base 10) system: 3 2 1 0 Human friendly 7 0 5 3 10 "#$ ! 𝑑% # 10% = 7 # 10& + 0 # 10' + 5 # 10$ + 3 # 10! = 7053 ! 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... "#$ ! 𝑑% # 2% = 1 # 2$' + 1 # 2$$ + 0 # 2 + … + 1 # 2 = 7053 $! ! ! Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 15 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, for I/O purposes only, we need efficient 1 1 1 7 algorithms for converting 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 / Copyright © Noam Nisan and Shimon Schocken Slide 16 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 ( 110 101 2 ) = 2! + 2" +2# +2$ = 53 10 23 = 8 24 = 16 Decimal to binary: 25 = 32 5 4 3 2 1 0 26 = 64 + 2" +2# +2$ binary ( 53 10 ) = 2! = 1 10 101 2 27 = 128 28 = 256 Algorithm: What is the largest power of 2 that “fits into” 53? It’s 25 = 32. 29 = 512 We still have to represent 53 – 32, so, what is the largest power of 2 that fits into 21? It’s 24 = 16, and so on. 210 = 1024... Practice: decimal ( 101 10 10 2 ) = ? binary ( 52 310 ) = ? Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 17 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 ( 110 101 2 ) = 2! + 2" +2# +2$ = 53 10 23 = 8 24 = 16 Decimal to binary: 25 = 32 5 4 3 2 1 0 26 = 64 + 2" +2# +2$ binary ( 53 10 ) = 2! = 1 10 101 2 27 = 128 28 = 256 Algorithm: What is the largest power of 2 that “fits into” 53? It’s 25 = 32. 29 = 512 We still have to represent 53 – 32, so, what is the largest power of 2 that fits into 21? It’s 24 = 16, and so on. 210 = 1024... Practice: decimal ( 101 10 10 2 ) = 90 10 binary ( 52 310 ) = 10 000 0 1011 2 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 18 The binary system Inside computers, everything is binary G.W. Leibnitz (1646 – 1716) Binary numerals are easy to: Compare Verify Add Correct Subtract Store Multiply Transmit Divide Compress Leibnitz Medallion, 1697...... Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 19 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 / Copyright © Noam Nisan and Shimon Schocken Slide 20 Boolean arithmetic We have to figure out efficient ways to perform, on binary numbers: Addition We’ll implement addition using logic gates Subtraction We’ll get it for free Multiplication We’ll implement it using addition Division Addition is the foundation of all arithmetic. Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 21 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 / Copyright © Noam Nisan and Shimon Schocken Slide 22 Addition Computers represent integers using a fixed number of bits. For example, let’s assume n = 4: 0 0 1 0 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 Another example Overflow Handling overflow Our approach: Ignore it As we’ll soon see, ignoring the overflow bit is not a bug, it’s a feature. Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 23 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. (Later). Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 24 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 / Copyright © Noam Nisan and Shimon Schocken Slide 25 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 / Copyright © Noam Nisan and Shimon Schocken Slide 26 Signed integers negative zero positive... -4 -3 -2 -1 0 1 2 3 4... In high-level languages, signed integers are typically represented using the data types short, int, and long (16, 32, and 64 bits) 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 are essential for building efficient computers. Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 27 Signed integers code(x) x 0000 0 0 This particular example: n = 4 0001 1 1 0010 2 2 In general, n bits allow representing 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 / Copyright © Noam Nisan and Shimon Schocken Slide 28 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 non-negative 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 / Copyright © Noam Nisan and Shimon Schocken Slide 29 Two’s complement code(x) x Representation (using n bits) 0000 0 0 The “two’s complement” of x is defined to be 2n – x 0001 1 1 The negative of x is coded by the two’s complement of x 0010 2 2 0011 3 3 From decimal to binary: 0100 4 4 if x ≥ 0 return binary(x) 0101 5 5 0110 6 6 else return binary(2n – x) 0111 7 7 From binary to decimal: 1000 8 –8 1001 9 –7 if MSB = 0 return decimal(bits) 1010 10 –6 else return “–” followed by (2n – decimal(bits)) 1011 11 –5 1100 12 –4 1101 13 –3 1110 14 –2 1111 15 –1 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 30 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 / Copyright © Noam Nisan and Shimon Schocken Slide 31 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 –2 1101 1110 13 14 –3 –2 + –4 = ? 1111 15 –1 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 32 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 / Copyright © Noam Nisan and Shimon Schocken Slide 33 Two’s complement: Addition code(x) x At the binary level (same algorithm): 0000 0 0 6 0110 Ignoring the overflow bit 0001 1 1 + = + –2 1110 is the binary equivalent of 0010 2 2 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 / Copyright © Noam Nisan and Shimon Schocken Slide 34 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 ??? 1001 9 –7 1001 –7 1010 10 –6 + = + –3 1101 1011 11 –5 1100 12 –4 10110 codes 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 an overflow. Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 35 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 / Copyright © Noam Nisan and Shimon Schocken Slide 36 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 / Copyright © Noam Nisan and Shimon Schocken Slide 37 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 / Copyright © Noam Nisan and Shimon Schocken Slide 38 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 / Copyright © Noam Nisan and Shimon Schocken Slide 39 Two’s complement: Recap code(x) x Observations 0000 0 0 Using n bits, 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 0110 6 6 unsigned integers 0111 7 7 Addition / subtraction / conversion are O(n) 1000 8 –8 Simple! Elegant! Powerful! 1001 9 –7 1010 10 –6 Implications for hardware designers 1011 11 –5 Arithmetic on signed integers can be implemented 1100 12 –4 using the same hardware used for handling arithmetic of 1101 13 –3 unsigned integers 1110 14 –2 1111 15 –1 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 40 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 / Copyright © Noam Nisan and Shimon Schocken Slide 41 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 / Copyright © Noam Nisan and Shimon Schocken Slide 42 Von Neumann Architecture Computer System CPU ALU Input Memory Output Registers Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 43 The Arithmetic Logical Unit f The ALU computes a given function on two given inputs, input1 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: Functions not implemented by the ALU can be implemented later by software Hardware implementations: Faster, more expensive Software implementations: Slower, less expensive. Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 44 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 / Copyright © Noam Nisan and Shimon Schocken Slide 45 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 ALU 16 bits out y+1 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 / Copyright © Noam Nisan and Shimon Schocken Slide 46 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 ALU 16 bits out y+1 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 / Copyright © Noam Nisan and Shimon Schocken Slide 47 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 Which function to compute is set by six 1-bit inputs 0 1 -1 x zx nx zy ny f no y !x !y -x -y x x+1 16 bits ALU 16 bits out y+1 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 / Copyright © Noam Nisan and Shimon Schocken Slide 48 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 ALU 16 bits out 1 0 1 0 0 1 1 1 1 1 1 0 y+1 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 / Copyright © Noam Nisan and Shimon Schocken Slide 49 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 ALU 16 bits out 1 0 1 0 0 1 1 1 1 1 1 0 y+1 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 / Copyright © Noam Nisan and Shimon Schocken Slide 50 The Hack ALU in action: Compute y-x Load tools/builtInChips/ALU.hdl Built-in ALU visualization Built-in ALU implementation Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 51 The Hack ALU in action: Compute y-x 2. Evaluate the chip logic 3. Inspect the ALU outputs 1. Set the ALU’s inputs and control bits to some test values (000111 codes “output y-x”) Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 52 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 ALU 16 bits out 1 0 1 0 0 1 1 1 1 1 1 0 y+1 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 / Copyright © Noam Nisan and Shimon Schocken Slide 53 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 / Copyright © Noam Nisan and Shimon Schocken Slide 54 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 / Copyright © Noam Nisan and Shimon Schocken Slide 55 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 / Copyright © Noam Nisan and Shimon Schocken Slide 56 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 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 (don’t care) 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 Compute and post-set: 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 / Copyright © Noam Nisan and Shimon Schocken Slide 57 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 Following pre-setting: 0 0 1 1 1 1 -x 1 1 0 0 x: 1 0 0 1 0 1 -y 0 1 1 1 y: 1 1 0 0 01 x+1 1 1 0 1 Compute 1 and post-set: 1 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 / Copyright © Noam Nisan and Shimon Schocken Slide 58 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 Following 0 pre-setting: 0 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 Compute 1 and post-set: 1 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 / Copyright © Noam Nisan and Shimon Schocken Slide 59 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 x:0 0 1 0 1 (don’t care) 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 Compute and post-set: 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 / Copyright © Noam Nisan and Shimon Schocken Slide 60 The Hack ALU operation One more detail: zx nx zy ny f no x 16 bits zr = ((out == 0), 1, 0) ALU 16 bits out y 16 bits ng = ((out < 0), 1, 0) zr ng The zr and ng output bits will come into play when we’ll build the computer’s CPU, later in the course. Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 61 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 / Copyright © Noam Nisan and Shimon Schocken Slide 62 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 / Copyright © Noam Nisan and Shimon Schocken Slide 63 Half Adder a b sum carry 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 1 HalfAdder.hdl CHIP HalfAdder { Implementation tip IN a, b; Can be built from two gates built in project 1. OUT sum, carry; PARTS: // Put your code here: } Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 64 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 CHIP FullAdder { Implementation tip IN a, b, c; OUT sum, carry; Can be built from two half-adders. PARTS: // Put your code here: } Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 65 16-bit adder Add16.hdl The carry propagations are computed sequentially CHIP Add16 { How does it end up working? IN a, b; Wait for chapter / lecture 3. OUT out; PARTS: // Put you code here: } Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 66 16-bit adder Add16.hdl CHIP Add16 { Implementation tip IN a, b; OUT out; To set a pin x to 0 (or 1) in HDL, PARTS: use: x = false (or x = true) // Put you code here: } Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 67 16-bit incrementor Inc16.hdl CHIP Inc16 { Implementation tip IN in; OUT out; To set a bus-subset x[i..j] to 00...0 (or to 11...1) in HDL, PARTS: use: x[i..j] = false (or x[i..j] = true) // Put you code here: } Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 68 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 / Copyright © Noam Nisan and Shimon Schocken Slide 69 ALU zx nx zy ny f no x 16 bits ALU 16 bits out y 16 bits zr ng ALU.hdl // 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 / Copyright © Noam Nisan and Shimon Schocken Slide 70 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 16 bits y 16 bits Setting a 16-bit value to 1111111111111111 Negating a 16-bit value (bitwise) Computing Add and And on two 16-bit values zr ng ALU.hdl // 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 / Copyright © Noam Nisan and Shimon Schocken Slide 71 Useful 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 / Copyright © Noam Nisan and Shimon Schocken Slide 72 Useful 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 CHIP Add16 { IN a, b; OUT out; PARTS: CHIP Foo {... IN x, y, z Another example of assigning } a multi-bit value to a sub-bus OUT out 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 / Copyright © Noam Nisan and Shimon Schocken Slide 73 ALU: Recap zx nx zy ny f no x 16 bits ALU 16 bits out y 16 bits zr ng To implement the ALU logic: We need to know how to... Implement “if bit == 0/1” conditions Set a 16-bit value to 0000000000000000 Set a 16-bit value to 1111111111111111 All simple operations Negate a 16-bit value (bitwise) Compute Add and And on two 16-bit values Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 74 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 / Copyright © Noam Nisan and Shimon Schocken Slide 75 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 / Copyright © Noam Nisan and Shimon Schocken Slide 76 Project 2 Given: The chips built in Project 1 Goal: Build the chips: HalfAdder FullAdder Add16 Inc16 ALU Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 77 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 / Copyright © Noam Nisan and Shimon Schocken Slide 78 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 Simple rule: Don’t add any HDL files to the project 2 folder. That’s It! Go Do Project 2! Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 79 What’s next? abstraction machine language This lecture / chapter / project: assembler p6 Build the computer’s ALU abstraction p4 p5 hardware platform building a p2 p3 computer abstraction computer building p1 ALU, RAM abstraction chips building elementary d logic gates gates Nan Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 80 What’s next? abstraction machine language This lecture / chapter / project: assembler p6 Build the computer’s ALU abstraction p4 p5 hardware platform building a p2 p3 computer abstraction computer building p1 ALU, RAM abstraction chips building elementary d logic gates gates Nan Next lecture / chapter / project: Build the computer’s RAM Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright © Noam Nisan and Shimon Schocken Slide 81