1 A Simple ALU Binary Logic. 2 Outline l Binary Logic l Representation of Logic Gates l Constructing...

of 23 /23
A Simple ALU Binary Logic

Embed Size (px)

Transcript of 1 A Simple ALU Binary Logic. 2 Outline l Binary Logic l Representation of Logic Gates l Constructing...

  • Slide 1

1 A Simple ALU Binary Logic Slide 2 2 Outline l Binary Logic l Representation of Logic Gates l Constructing a 1-bit adder l Constructing an n-bit adder Slide 3 3 Binary Logic l A computer works using digital electronics. l There are only High and Low voltages l All computer logic is based on the manipulation of these l Abstractly, High is interpreted as 1 (true) Low is interpreted as 0 (false) Slide 4 4 Logic Circuit l A logic circuit takes in a number of input lines (A, B, C,...) and has a number of output lines (X,Y,Z,...) A B C X Y Z Slide 5 5 Logic Basics l Every expression evaluates to 1 or 0 l NOT ~A (A bar) is the complement of A, i.e. 1-A l OR A + B is 1 if and only if at least one of A or B is 1, else 0 l AND A.B is 1 if and only if both A and B are 1, else 0 Slide 6 6 Truth Tables ABA+BA.B 0000 0110 1010 1111 A~A 01 10 ABCA.BA.CB+CA.(B+C) A.B+A.C 00000 0 0 0 00100 1 0 0 01000 1 0 0 01100 1 0 0 10000 0 0 0 10101 1 1 1 11010 1 1 1 11111 1 1 1 A.(B+C)=A.B+A.C Slide 7 7 Gates A B A.BAND Gate A B A+B OR Gate A ~AInverter (NOT) Slide 8 8 Representations of Integers l Positive integers are represented by strings of binary digits - of fixed length given by the word length (eg, 32 bits). l Numbers in the range 0 to 2 32 -1 can be represented. 0131 LSBMSB Slide 9 9 Negative Numbers l We have to have a way to represent negative numbers and 0. If the word length is n, then there are 2 n bit-patterns possible - there cannot be an equal number of positive and negative numbers represented. l Suppose n=3, and consider the following: 000 two = 0 ten 100 two = -4 ten 001 two = 1 ten 101 two = -3 ten 010 two = 2 ten 110 two = -2 ten 011 two = 3 ten 111 two = -1 ten Slide 10 10 Twos Complement l For an n-bit word, the MSB is called the sign bit, which if 1 determines a negative number. l An n-bit word: (b n-1 b n-2...b 1 b 0 ) two represents the decimal number: l Eg, if n=4, 1101 = -8 + 4 + 1 = -3. l Eg, if n=5, 10110 = -16 + 2 + 4 = -10 Slide 11 11 Negating a Number l A simple technique for negating a number: Invert each bit, and then add 1. l Convert:- 10110 01001 + 1 = 01010 = 2 ten +8 ten = 10 ten 1101 0010 + 1 = 0011 = 1 ten + 2 ten = 3 ten 0011 1100 + 1 = 1101 = -8 ten +4 ten +1 ten = -3 ten Slide 12 12 Arithmetic l Binary arithmetic, addition and subtraction are similar to decimal: work from least significant bit to most significant bit using carries where necessary. l A fundamental issue on a computer is the fixed word size The operands and result is dependent on the word length, and sometimes the result will be too small (negative) or too large (positive) to be represented. Slide 13 13 Addition and Subtraction l Examples, n=4 (OVERFLOW) 0110+6 ten 01117 ten ------------- 1101 = -8+4+1 = -3 ten l Subtraction 0111-0110 = 0111+1010 (twos complement) = 0001 = 1 ten Slide 14 Overflow (Patterson) Examples: 7 + 3 = 10 but... - 4 - 5 = - 9 but... 2s ComplementBinaryDecimal 00000 10001 20010 30011 0000 1111 1110 1101 Decimal 0 -2 -3 40100 50101 60110 70111 1100 1011 1010 1001 -4 -5 -6 -7 1000-8 0111 0011+ 1010 1 1100 1011+ 0111 110 7 3 1 - 6 - 4 - 5 7 Slide 15 Overflow Detection (Patterson) Overflow: the result is too large (or too small) to represent properly Example: - 8 < = 4-bit binary number