Presentation is loading. Please wait.

Presentation is loading. Please wait.

EGR 2131 Unit 6 Number Representation and Arithmetic Circuits

Similar presentations


Presentation on theme: "EGR 2131 Unit 6 Number Representation and Arithmetic Circuits"— Presentation transcript:

1 EGR 2131 Unit 6 Number Representation and Arithmetic Circuits
Read Brown & Vranesic, Chapter 5 and Sections A.5 and A.6. Midterm Exam next week. Homework #6 and Lab #6 due in two weeks. -Preview Midterm Exam. Handouts: Quiz, Unit 6 practice sheet. Fire up Quartus.

2 The Hexadecimal and Octal Systems
We’ve looked at the decimal and binary systems. Hexadecimal (often called “hex”) and octal are useful as shorthand systems for writing long strings of bits. Hex is a base-16 system. Hex is very widely used. Octal is a base-8 system. Octal was popular 40 years ago, but is not used much today. It’s not convenient to write (or speak) a long binary number such as Much more convenient to express it in hex as 32B57. Fill in Hexadecimal column of practice question 1.

3 Example Solution Hexadecimal Numbers
Binary Hexadecimal Numbers A B C D E F Hexadecimal uses sixteen characters to represent numbers: the digits 0 through 9 and the letters A through F. Large binary numbers can easily be converted to hexadecimal by grouping bits 4 at a time (starting from the right-hand end) and writing the equivalent hex character. -They must memorize this table. Do practice questions 2 and 3. Express in hexadecimal: Example Solution Group the binary number by 4-bits starting from the right. Thus, 960E

4 { Example Solution Hexadecimal Numbers
Hexadecimal is a weighted number system. The column weights are powers of 16, which increase from right to left. { Column weights Example Express 1A2F16 in decimal. Do practice question 4. Start by writing the column weights: Solution A F16 1(4096) + 10(256) +2(16) +15(1) = 670310

5 Binary and Hex Conversion Games
You must memorize the binary and hex codes for the numbers from 0 through 15. To practice, play the Binary-Decimal and Binary-Hex matching games on my Games page.

6 Example Solution Octal Numbers
Decimal Octal Binary Octal Numbers Octal uses eight characters to represent numbers: the digits 0 through 7. Large binary numbers can easily be converted to octal by grouping bits 3 at a time (starting from the right-hand end) and writing the equivalent octal character. Fill in octal column of practice question 1. Then do this slide’s example as practice question 5. Express in octal: Example Solution Group the binary number by 3-bits starting from the right:

7 Octal Numbers Octal is a weighted number system. The column weights are powers of 8, which increase from right to left. { Column weights

8 Decimal Binary BCD BCD Binary coded decimal (BCD) is a weighted code that is commonly used in digital systems when it is necessary to show decimal numbers such as in clock displays. The table illustrates the difference between straight binary and BCD. BCD represents each decimal digit with a 4-bit code. Notice that the codes 1010 through 1111 are not used in BCD. -BCD is a “compromise” between binary (which is most convenient for computers) and decimal (most convenient for humans). -Point out that seven-segment displays on red trainer are labeled “BCD.” Fill in BCD column of practice question 1. Then do practice question 6.

9 BCD You can think of BCD in terms of column weights in groups of four bits. For an 8-bit BCD number, the column weights are: Question: What are the column weights for the BCD number ? Answer: Note that you could add the column weights where there is a 1 to obtain the decimal number. For this case: =

10 Binary Arithmetic Now that we’ve looked at some different ways to represent numbers, let’s look at how to add two binary numbers. Example: what is the result of adding plus ? One way to do this manually would be to convert both numbers to decimal and then add, using our lifelong knowledge of decimal arithmetic. Instead, let’s learn the rules for adding in binary.

11 The rules for binary addition are 0 + 0 = 0 Sum = 0, carry out = 0
When a carry in = 1 due to a previous result, the rules are = 01 Sum = 1, carry out = 0 = 10 Sum = 0, carry out = 1 -Recall learning addition & subtraction rules as a kid ( ; ). -Binary is similar, but with far fewer possible combinations of 1-digit numbers. -First, 4 flashcards with no carry; then 4 with carry=0 and 4 with carry=1. = 10 Sum = 0, carry out = 1 = 11 Sum = 1, carry out = 1

12 Example Solution Binary Addition
Add the binary numbers and and show the equivalent decimal addition. Example Solution 1 1 1 1 1 1 = 28 Do Practice Question 7.

13 Half-Adder A half adder circuit performs the basic rules of binary addition with no carry-in. It has two inputs (x and y) and two outputs (c and s).

14 Full-Adder A full adder circuit performs the basic rules of binary addition with carry-in. It has three inputs (x, y, cin) and two outputs (cout and s). The Boolean expressions for the outputs are not obvious, but we can easily use K-maps to find them (as the book does.) See next slide….

15 Full-Adder (Cont’d.) Here is one way to build a full adder.
We’ll use this graphical symbol for a full adder. This is fine for doing one column in the addition of two multi-bit numbers. For the entire addition, you’ll need as many of these circuits as there are columns in your numbers.

16 Conventions for Labeling the Bits in Multi-Bit Numbers
We’ll use subscripts to refer to the individual bits in a binary number. The bit on the right-hand end, or least significant bit (LSB), always gets the smallest subscript, which may be either 1 or 0. Example: In a four-bit number x, the bits are labeled either x4x3x2x1 or x3x2x1x0 -When we have multi-bit numbers, we start numbering (at 0 or 1) from right. Don’t confuse these ID numbers with the column weights. Our book usually follows this convention. 16

17 Parallel Adder A full adder is fine for adding two 1-bit numbers (plus a carry-in). To add two multi-bit numbers (plus a carry-in) we can combine full adders to make a parallel adder. -Earlier, inputs were shown on the left and outputs on the right. Here inputs are shown on the top/left and outputs on the bottom/right. -We’re adding two n-bit numbers. -Do Practice Question 8. 17

18 Ripple versus Lookahead
A drawback to the design shown is that the carries must “ripple” through all the stages of the adder before the outputs settle to their final values. This design is called a ripple-carry adder. An alternative design, also discussed in the book, is called a carry-lookahead adder (or fast adder). It produces a result faster.

19 Parallel Adder Symbol Here is a common graphical symbol for an n-bit parallel adder. (Its internal design could be either carry-ripple or carry-lookahead.) 19

20 A 74xx Adder Chip 7483 Four-bit binary adder
If you’re just using it to add two 4-bit numbers, you’ll tie C0 to ground. If you’re adding two numbers with more than 4 bits, you’ll use more than one of these adders connected together. Do Practice Question 9.

21 Cascading Adder Chips When we connect the outputs from one chip to the inputs of another identical chip to expand the number of bits being operated on, we say that the chips cascaded together. Example: to add two 8-bit numbers, you can cascade two 4-bit adder chips. To do this, connect the lower-order chip’s Carry Out to the higher-order chip’s Carry In. (See next slide.)

22 8-bit Adder Using Two 4-bit Chips
This sort of arrangement was common in the old days. But modern FPGA chips can hold the equivalent of thousands of 7400-series chips, so it’s less common today.

23 Designing Arithmetic Circuits in Quartus
As you know, Quartus gives you two general ways to enter designs: Schematic capture Here you can work at the gate level, or you can place 74XX chips such as the 7483 adder. But Quartus also gives you more powerful and flexible options. Text entry with an HDL such as VHDL. VHDL has robust support for arithmetic operations and makes it easy to design complicated arithmetic circuits. 23

24 Designing Arithmetic Circuits in Quartus Method #1: Schematic Capture
Quartus and similar CAD tools offer predefined subcircuits that you can place on a schematic diagram and customize to meet your needs. Quartus uses the name “Library of Parameterized Modules” for its collection of predefined subcircuits. The items in this collection all have names that start with “lpm,” such as “lpm_add_sub.” 24

25 Designing Arithmetic Circuits in Quartus Method #1: Schematic Capture (Cont’d.)
Here’s is an example of an lpm_add_sub that has been customized to operate on 16-bit numbers and to have certain optional inputs and outputs, such as Carryin and Overflow. Doing this with individual gates would require you to place and connect dozens of gates on the diagram. 25

26 Designing Arithmetic Circuits in Quartus Method #2: Text Entry with VHDL
VHDL offers several ways to design arithmetic circuits. We’ll look at two: Building up a complicated structure (such as a 16-bit parallel adder) out of many simpler structures (such as a full adder). Using arithmetic operators (such as +) in assignment statements (such as S <= X + Y). 26

27 Designing Arithmetic Circuits in Quartus Method #2: Text Entry with VHDL (Cont’d.)
Recall that you can design an n-bit parallel adder by combining many copies of a much simpler circuit called a full adder. The full adders are all identical to each other, and each one contains only a few gates. 27

28 Designing Arithmetic Circuits in Quartus Method #2: Text Entry with VHDL (Cont’d.)
In VHDL, a COMPONENT declaration statement and several component instantiation statements work together to let you define a subcircuit once and then “copy and paste” it many times as part of a bigger circuit. 28

29 VHDL Code for a Full Adder
First, we can easily write VHDL code to implement a full adder. Nothing new here. 29

30 VHDL Code for 4-Bit Adder: ENTITY
Here is the entity part of the design for a 4-bit adder that will contain four copies of the full adder. Nothing new here. 30

31 VHDL Code for 4-Bit Adder: ARCHITECTURE
A COMPONENT declaration looks a lot like an ENTITY declaration. Each of these statements instantiates a copy of the component described in the COMPONENT declaration above. 31

32 The STD_LOGIC_VECTOR Data Type
You could extend the code on the previous three slides from 4 bits to any number of bits—let’s say, 16. But that would mean revising the PORT statement (repeated below) to declare many signals x15, x14, …, x0, y15, y14, …, y0, s15, s14, …, s0. VHDL provides a better way to declare and use multi-bit signals…. 32

33 The STD_LOGIC_VECTOR Data Type (Cont’d.)
Instead of declaring four STD_LOGIC signals named x3, x2, x1, x0, you can declare one STD_LOGIC_VECTOR signal that contains 4 bits. Instead of this: Do this: 33

34 The STD_LOGIC_VECTOR Data Type (Cont’d.)
Having declared a STD_LOGIC_VECTOR signal containing many bits, you can refer to a single bit within that signal by writing X(3), X(2), etc. Also, use double quotes to assign a constant value to a vector: X <= "0101"; Or use single quotes to assign constant values to the individual bits in a vector: X(3) <= '0'; X(2) <= '1'; X(1) <= '0'; X(0) <= '1'; 34

35 The STD_LOGIC_VECTOR Data Type (Cont’d.)
So here is the revised architecture of our 4-bit adder, using STD_LOGIC_VECTOR signals: 35

36 Arithmetic Operators in VHDL
The previous 8 or 10 slides have discussed how to build up a complex circuit (such as a 4-bit adder) from a simple component (such as a full adder). It’s important for you to understand this so-called structural approach or hierarchical approach to VHDL code, since it is widely used. However, VHDL supports arithmetic operators (such as + and –) that make it much easier to design arithmetic circuits. 36

37 A 16-Bit Adder Using VHDL Arithmetic Operators
To use the arithmetic operators you must include this or ieee.std_logic_unsigned.all Can’t get any easier than this! 37

38 Terminology: One’s-Complement and Two’s-Complement
The one’s-complement of a binary number is the binary number that you get when you invert each bit. Example: What is the one’s-complement of ? The two’s-complement of a binary number is the binary number that you get when you invert each bit and then add 1 to the result. Example: What is the two’s-complement of ? Do as Practice Questions 10 and 11. 38

39 Interpreting Strings of 1s and 0s
In digital systems we have nothing but 1s and 0s to represent all kinds of info: text, numbers, images, music, etc. To interpret a string of 1s and 0s, you have to be told what kind of info it represents. Example: What does represent? -Open a text file and a JPG file in Windows Notepad. Possible answers: The integer 65. The letter A (in ASCII code) A particular color in a JPEG file. … 39

40 Unsigned versus Signed Binary Integers
Up to now, whenever we’ve worked with binary integers, we’ve assumed they were unsigned binary integers. In other words, we’ve assumed that the integers were all positive. Sometimes we also need to be able to represent negative integers, in which case we’re dealing with signed binary integers. To do this, we’ll use one bit to indicate the integer’s sign (positive or negative). -So when you interpret a string of 1s and 0s as a number, you’ll also need to know whether it represents an unsigned integer or a signed integer. -Before we see how to do this, let’s review what we know about unsigned integers. 40

41 Range of Unsigned Binary Integers
Arranging unsigned 8-bit integers in order from least to greatest would give you a list that starts and ends like this: Binary Decimal   Do Practice Question 12. 41

42 Range of Unsigned Binary Integers (Cont’d.)
For unsigned integers with a fixed number of bits n: The least integer we can represent is 0. The greatest integer we can represent is 2n-1. Example: Using 8 bits, The least integer is , which is equal to decimal 0. The greatest integer is , which is equal to decimal 255 (=28-1). Do Practice Questions 13, 14. 42

43 Representing Signed Binary Integers
To represent both positive and negative integers, we’ll use the leftmost bit as a sign bit, like this (for 8 bits): Binary Decimal     Negative integers (sign bit = 1) Do Practice Question 15. Zero and positive integers (sign bit = 0) 43

44 Two’s-Complement Representation
The scheme shown on the previous slides is called two’s-complement representation. This is how almost all computers represent signed integers, because this scheme results in simple circuits for doing binary arithmetic. Other common names for it: two’s complement notation or two’s complement form. 44

45 Range of Signed Binary Integers
For signed integers with a fixed number of bits n, the least integer we can represent is −2n−1, and the greatest integer we can represent is 2n−1−1. Example: Using 8 bits, The least integer we can represent is , which is equal to decimal −128 (= −27). The greatest integer we can represent is , which is equal to decimal 127 (= 27-1). Do Practice Question 16, 17. 45

46 Other Possible Ways of Representing Negative Integers
Over the years, people have developed several ways of representing negative integers in binary: Sign-magnitude representation One’s-complement representation Two’s-complement representation The book discusses all three of these. But two’s-complement representation is used in almost all computer circuits today, so we’ll ignore the other two. The easiest for humans. The best for computers. 46

47 Steps for Converting from Decimal to Two’s-Complement Form
If the integer is positive, convert it to binary as we’ve always done. If the integer is negative: Ignoring the sign, convert it to binary as we’ve always done. Invert each bit. (That is, change each 0 to 1, and change each 1 to 0.) Add 1 to the result. Examples Convert 2310 to two’s complement form using 8 bits. Convert −2310 to two’s complement form using 8 bits. -This is how our book tells you to do it. There are other ways to do it, which some people find easier to remember. If you’re interested, I can show you. Do as Practice Question 18. 47

48 Steps for Converting from Two’s-Complement Form to Decimal
If the sign bit = 0, the integer is positive. Convert to decimal as we’ve always done. If the sign bit = 1, the integer is negative. Follow these steps: Invert each bit. Add 1. Convert the result to decimal as we’ve always done. Write a negative sign in front. Examples: Convert to decimal. Convert to decimal. -This is how our book tells you to do it. There are other ways to do it, which some people find easier to remember. If you’re interested, I can show you. -Do as Practice Question 19. 48

49 The Windows Calculator Uses Two’s-Complement Representation
49

50 Adding Signed Numbers Using two’s complement form for negative integers simplifies addition and subtraction of signed numbers. Rules for addition: Add the two signed numbers, including their sign bit. Discard any final carry out of the MSB. The result is in two’s complement form. Examples: 45 -3 1 -9 Discard carry

51 Binary Subtraction To do binary subtraction by hand, you could memorize rules similar to the rules for addition. But this can be a bit tricky because of the need to borrow in some cases. A better way: Negate the subtrahend, and then add to the minuend. This should be a familiar idea, since you know that subtracting 12 minus 5 is the same as adding 12 plus -5. So instead of needing new rules for subtraction, you just need to combine two earlier skills: Negate a number (by taking its 2’s-complement). Add binary numbers. 51

52 Subtracting Signed Numbers
Rules for subtraction: Negate the number being subtracted, and then add. Discard any final carry out of the MSB. The result is in two’s complement form. The same numbers as on previous slide, but subtracting: - 30 – 15 14 – -17 -1 – -8 Negate the number being subtracted, and then add: Do Practice Question 20. 1 15 31 1 7 Discard carry Discard carry

53 A Useful Visual Aid Two’s-complement notation may be confusing the first time you see it. The image below (which assumes we’re using a 4-bit system but extends easily to more bits) may be helpful. Addition carries you clockwise around the circle. Subtraction carries you counter-clockwise. 53

54 Adder/Subtractor Circuit
By combining an adder with a controlled inverter, we can make a circuit that either adds or subtracts, depending on the value of a control input. See the book’s Figure 5.13 (next slide). 54

55 Adder/Subtractor Circuit (Cont’d.)
Common convention used here: the label 𝐀𝐝𝐝 /𝐒𝐮𝐛 indicates that this input should be set to 0 when you want to add, set to 1 when you want to subtract. A controlled inverter! 55

56 Arithmetic Overflow Overflow is a problem that can arise in arithmetic circuits that perform two’s-complement addition or subtraction. Overflow occurs when the correct result falls outside the range of values that can be represented using the number of bits available. Looking at it another way, overflow occurs when an operation carries you past the discontinuity between positive and negative numbers on the circle. 56

57 Overflow: Example Suppose we’re using 8-bit two’s-complement notation and we try to add We know the result should be 135, but the maximum number we can represent in 8-bit two’s-complement notation is 127. In binary, it looks like this: -121 ?!?! Wrong! It’s obvious from the sign bits that something went wrong here because we’re adding two positive numbers but we got a negative result. 57

58 Detecting Overflow There are a few ways to determine whether a 2’s-complement computation has resulted in an overflow. Probably the most widely used is: if there’s a carry into the sign bit but no carry out of the sign bit (or vice versa), then overflow has occurred and the result cannot be trusted. Do Practice Questions 21, 22. -121 ?!?! Wrong! In our previous example, note that there is a carry into the sign bit but no carry out of the sign bit. 58

59 Detecting Overflow (Cont’d.)
Recall that Overflow is one of the optional outputs that you can specify when you place an lpm_add_sub in a Quartus schematic file. 59

60 Additional Topics Here are three interesting topics discussed in Chapter 5 that we don’t have time for: Designing fast adders (carry-lookahead adders) that produce results more quickly than the ripple-carry adder we looked at. Designing multiplier circuits. Representing non-integer numbers (such as 36.75) in binary.


Download ppt "EGR 2131 Unit 6 Number Representation and Arithmetic Circuits"

Similar presentations


Ads by Google