ENEL 353 F13 Section 02 Slides for Lecture 5 slide 2/19 ENEL 353 F13 Section 02 Slides for Lecture 5 slide 3/19 Previous Lecture Today’s Lecture Slides for Lecture 5 ENEL 353: Digital Circuits — Fall 2013 Term ◮ ranges for two’s-complement systems ◮ overflow in two’s-complement addition ◮ BCD (binary coded decimal) codes for decimal digits Steve Norman, PhD, PEng ◮ drawbacks of sign/magnitude representation of signed integers ◮ Gray codes Electrical & Computer Engineering ◮ two’s complement representation of signed integers Schulich School of Engineering University of Calgary Related reading in Harris & Harris: Section 1.4.6 for two’s-complement concepts and facts. Harris & Harris do not 18 September, 2013 really cover BCD or Gray codes—there are very brief descriptions on pages 258 (BCD) and 76 (Gray codes). slide 4/19 slide 5/19 slide 6/19 ENEL 353 F13 Section 02 Slides for Lecture 5 ENEL 353 F13 Section 02 Slides for Lecture 5 ENEL 353 F13 Section 02 Slides for Lecture 5 Sum of a finite geometric series Ranges for two’s-complement systems Overflow in two’s-complement addition (often called “signed overflow”) A fact from mathematics: For any r , For an n -bit two’s-complement system, the minimum value is − (2 n − 1 ) and the maximum value is 2 n − 1 − 1. Here are some k − 1 r i = 1 + r + r 2 + . . . + r k − 2 + r k − 1 = r k − 1 . � example ranges for various choices of n : i =0 width n minimum value maximum value What is the largest number in a 6-bit two’s-complement − 2 3 = − 8 What does that say about the largest (“most positive”) 4 2 3 − 1 = 7 system? number in an n-bit two’s-complement system? 8 ? ? So, something will go wrong if we try to add 23 10 + 23 10 in 16 ? ? What if we negate the largest number? Does that give us the 6-bit two’s complement. Let’s see what happens . . . 32 − 2 , 147 , 483 , 648 2 , 147 , 483 , 647 smallest (“most negative”) number in the system? (Attention: In the interest of saving time, ENEL 353 lectures The last row in the table gives the range of a typical C or C ++ skip the proofs of key facts about how two’s complement int type. works.) ENEL 353 F13 Section 02 Slides for Lecture 5 slide 7/19 ENEL 353 F13 Section 02 Slides for Lecture 5 slide 8/19 ENEL 353 F13 Section 02 Slides for Lecture 5 slide 9/19 Facts about overflow in two’s-complement addition Unsigned overflow vs. signed overflow in binary Unsigned overflow vs. signed overflow in binary addition addition: 4-bit examples Overflow cannot happen when two numbers with opposite Unsigned overflow and signed overflow are NOT THE SAME signs are added. In addition of 1100 and 1101 is there unsigned overflow? Is THING! there signed overflow? When two numbers with the same sign are added, overflow With an n -bit adder you can have neither, one but not the has occurred if and only if the sign of the result is What about in each of these three additions? other, or both. obviously wrong . Ways to detect unsigned overflow: carry: 1 0 0 0 0 carry: 1 1 1 1 0 carry: 0 1 1 0 0 Another way to detect overflow, not stated in Harris & Harris: ◮ Carry out of MSB column is 1. 1 1 0 0 1 1 1 1 0 1 1 0 Overflow has occurred if and only if the carry in to the MSB + 1 0 1 0 + 0 0 0 1 + 0 0 1 1 column does not match the carry out from that column. ◮ n -bit sum is smaller than either of the n -bit numbers 0 1 1 0 0 0 0 0 1 0 0 1 being added.
ENEL 353 F13 Section 02 Slides for Lecture 5 slide 10/19 ENEL 353 F13 Section 02 Slides for Lecture 5 slide 11/19 ENEL 353 F13 Section 02 Slides for Lecture 5 slide 12/19 Codes BCD: Binary Coded Decimal An application of BCD encoding: Simple calculators decimal BCD code digit “word” 0 0000 In this course, a code is a system for representing numbers or Keyboard input is base ten, and so is 1 0001 symbols with sequences of 1’s and 0’s. Each digit in a BCD-encoded display output. To keep the design as 2 0010 decimal number is represented Codes we’ve seen so far in this course: unsigned binary, simple as possible, it makes sense to 3 0011 by a 4-bit “word”. sign/magnitude, two’s complement. store numbers and do arithmetic in 4 0100 For example, in 16-bit BCD, base ten. Some other important codes: ASCII code for text, BCD code 5 0101 how would 1985 10 be encoded? for base ten numbers, Gray codes. 6 0110 7 0111 Photo from 8 1000 www.oldcalculatormuseum.com/us1.html 9 1001 slide 13/19 slide 14/19 slide 15/19 ENEL 353 F13 Section 02 Slides for Lecture 5 ENEL 353 F13 Section 02 Slides for Lecture 5 ENEL 353 F13 Section 02 Slides for Lecture 5 How each digit in the calculator is displayed BCD Arithmetic Algorithms, Circuits and Software Gray codes Each digit uses a “7-segment display”—a collection of 7 LEDs. (More modern equipment uses LCDs instead of 3-bit 3-bit LEDs—LCDs use much less power.) unsigned Gray In an n -bit Gray code, each A BCD-based calculator needs logic circuits and number binary code code code word differs from the microprocessor software to do BCD arithmetic: addition, 0 000 000 previous one in only one bit BCD−to− subtraction, multiplication, division. 1 001 001 position. Also the first and seven− Past years’ versions of ENEL 353 spent a little time explaining 2 010 011 last codes differ in only one segment how to do BCD addition. Students in ENEL 353 in Fall 3 011 010 bit position. decoder 2013 will NOT be tested on BCD addition or any other 4 100 110 That is NOT true for n -bit 5 101 111 kind of BCD arithmetic. unsigned binary encoding! 6 110 101 7 111 100 4−bit BCD 7 bits turn segments word on or off ENEL 353 F13 Section 02 Slides for Lecture 5 slide 16/19 ENEL 353 F13 Section 02 Slides for Lecture 5 slide 17/19 ENEL 353 F13 Section 02 Slides for Lecture 5 slide 18/19 Construction of Gray codes Construction of Gray codes, continued 4-bit Gray code 1st half gets Duplicate previous Gray code in leading 0, 2nd half Start with 1-bit reverse order . . . gets leading 1 . . . number Gray code number Gray code Gray code . . . 0 0000 8 1100 incomplete complete Let’s use the construction algorithm to generate the 3-bit Gray 1 0001 9 1101 code. 1-bit 2-bit 2-bit 2 0011 10 1111 num- Gray num- Gray num- Gray Do it yourself: Starting with the 3-bit Gray code, use the 3 0010 11 1110 ber code ber code ber code construction algorithm to generate the 4-bit Gray code, and 4 0110 12 1010 0 0 0 0 0 00 5 0111 13 1011 check your work against the table on the next slide. 1 1 1 1 1 01 6 0101 14 1001 2 1 2 11 7 0100 15 1000 3 0 3 10 Repeat this process until the code has the desired number of bits.
ENEL 353 F13 Section 02 Slides for Lecture 5 slide 19/19 Next Lecture A little more about Gray codes. Introduction to logic gates. Related reading in Harris & Harris: Section 1.5
Recommend
More recommend