ENG241 Digital Design Week #5 Arithmetic Circuits
2Topics Binary Adders Binary Ripple Carry Adder 1’s and 2’s Complement Binary Subtraction Binary Adder-Subtractors Binary Multipliers BCD Arithmetic
3Resources Chapter #5, Mano Sections 5.2 Binary Adders 5.3 Binary Subtraction 5.4 Binary Adders-Subtractors 5.5 Binary Multiplications 5.7 HDL Representations -- VHDL
4 Recall: Arithmetic -- addition Binary addition is similar to decimal arithmetic No carries Carries Remember: 1+1 is 2 (or (10) 2 ), which results in a carry is 3 (or (11) 2 ) which also results in a carry
5 Half Adder (One bit adder) o S = XY’ + X’Y = X Y o C = X.Y
6 Full Adder o Three inputs: X Y Third is C in Z o Two outputs: Sum C out S Full Adder xy ZC out Implementation?
7 Straight Forward Implementation: What is this? Z S K Map for S
8 Y X Z Y Z X C Straight Forward Implementation: K Map for C
9 Implementation Issues how many gates o If we try to implement the Optimized Boolean functions directly we will need how many gates? Seven AND gates and two OR Gates!! o Can we do better? o YES!! Share Logic Hierarchical Design.
10 Any Alternatives? o Try to make use of hierarchy to design a 1-bit full adder from two half adders. o Also, try to share logic between the Sum output and Carry output. Half Adder S = X Y C = XY Full Adder S = X Y Z C = XY + XZ + YZ
11 A Different Way to Represent C X YZ XY XYZ C = XY + XYZ + XYZ C = XY + Z (XY + XY)
12 Two Half Adders (and an OR) How many Gates do we need? Full Adder xy ZC S
13 Binary Ripple-Carry Adder A Parallel binary adder is a digital circuit that produces the arithmetic sum of two binary numbers using only combinational logic. The parallel adder uses “n” full adders in parallel, with all input bits applied simultaneously to produce the sum. The full adders are connected in cascade, with the carry output from one full adder connected to the carry input of the next full adder.
14 Binary Ripple-Carry Adder Straightforward – connect full adders Carry-out to carry-in chain C 0 in case this is part of larger chain, maybe just set to zero
15 Hierarchical 4-Bit Adder We can easily use hierarchy here 1. Design half adder 2. Use TWO half adders to create full adder 3. Use FOUR full adders to create 4-bit adder VHDL CODE?
16 VHDL Half Adder (DATA FLOW) entity half_adder is port (x,y: in std_logic; s,c: out std_logic); end half_adder; architecture dataflow of half_adder is begin s <= x xor y; c <= x and y; end dataflow
17 VHDL Full Adder (Structural) entity full_adder is port (x, y, z: in std_logic; s, c: out std_logic); end full_adder; architecture struc_dataflow of full_adder is hs hc tc component half_adder port (x, y : in std_logic; s, c : out std_logic); end component; signal hs, hc, tc: std_logic; begin HA1: half_adder port map (x, y, hs, hc); HA2: half_adder port map (hs, z, s, tc); c <= tc or hc; end struc_dataflow
18 Any Problems with this Design? Delay Approx how much? Imagine a 64-bit adder Look at carry chain
19 Carry Propagation & Delay One problem with the addition of binary numbers is the length of time to propagate the ripple carry from the least significant bit to the most significant bit. The gate-level propagation path for a 4-bit ripple carry adder of the last example: Note: The "long path" is from A 0 or B 0 through the circuit to S 3.
20 BCD Addition One decimal digit + one decimal digit ●I●If the result is 1 decimal digit ( ≤ 9 ), then it is a simple binary addition Example: ●I●If the result is two decimal digits ( ≥ 10 ), then binary addition gives invalid combinations Example:
21 BCD Addition If the binary result is greater than 9, correct the result by adding a “6” Two Decimal Digits Multiple Decimal Digits
ENG241/Digital Design22 BCD Arithmetic 81000Eight Plus Five is 13 (> 9) Note that the result is MORE THAN 9, so must be represented by two digits! To correct the digit, add Eight Plus is 13 (> 9) so add 6 carry = leaving 3 + cy 0001 | 0011 Final answer (two digits) 22
23 BCD Addition Circuit Design a BCD Adder that adds two BCD digits. Constraints: Use 4-bit Binary Adders Hints: A detection circuit that detects invalid BCD digits will need to be designed.
ENG241/Digital Design24 BCD Addition 4-bit binary adder Addend Augend Input Carry 4-bit binary adder BCD Sum 0 or 6 Detection Circuit for Invalid BCD Output Carry 24
25 BCD Addition Z 3 Z 2 Z 1 Z 0
Subtraction We managed to design an Adder easily. Subtractor For subtraction, we will also need to design a Subtractor!! Can we perform subtraction using the Adder Circuit we designed earlier? YES, we can use the concept of Complements. X = Y – Z X = Y + complement(Z) 26
Complements? two types of complements There are two types of complements for each base-r system The radix complement, the (r’s) complement. The diminished radix complement, (r-1)’s comp. For Decimal System 10’s complement 9’s complement For Binary Systems 2’s complement 1’s complement 27
Complements of Decimal System The 9’s complement of a decimal number is obtained by subtracting each digit from 9. Example: The 9’s complement of is – = The 10’s complement is obtained by adding 1 to the 9’s complement: Example: The 10’s complement of is – = = Or, – = Or, leave all least significant 0’s unchanged, subtract the first nonzero LSD from 10, and subtract all higher significant digits from 9. 28
Unsigned Decimal Subtraction – 3250 = Use 10’s complement to perform the subtraction M = (5-digits), N = 3250 (4-digits) Since N has only 4 digits append a zero N=03250 What is the 10’s complement of N (03250)? + 1 – = = Now add Now add M to the 10’s comp of N = (carry occurred) The occurrence of the end carry indicates that M > N Discard end carry ( – = 69282) 29 Example #1
= (HOW??) Compare the numbers, exchange their positions, … Use 10’s complement to perform the subtraction M = 3250 (4-digits), N = (5-digits) Since M has only 4 digits append a zero M=03250 What is the 10’s complement of N (72532)? – = = Now add Now add M to the 10’s comp of N = (There is no end carry!) No end carry indicates that M < N (make correction!!) Answer: -(10’s complement of 30718) = Unsigned Decimal Subtraction Example #2
31 Binary Subtraction We’ll use unsigned subtraction to motivate use of complemented representation
32 1’s Complement 11 ’s Complement (Diminished Radix Complement) ●A●All ‘0’s become ‘1’s ●A●All ‘1’s become ‘0’s Example ( ) 2 ( ) 2 If you add a number and its 1’s complement …???
33 1’s Complement: Example Notice that the 1’s complement of the number can be obtained by complementing each bit 2 n N ’s Compl
34 2’s Complement 22 ’s Complement (Radix Complement) ●T●Take 1’s complement then add 1 ●T●Toggle all bits to the left of the first ‘1’ from the right Example: Number: 1’s Comp.: OR
35 2’s Complement: Example complementingadding Notice that the 2’s complement of the number can be obtained by complementing each bit and adding 1. 2n2n N ’s Comp ’s Compl
36 Example: Incorrect Result Borrow11100 (M) Minuend (N) Subtrahend Difference – 30 = 21 !!!!! Incorrect Result!! Minuend is smaller than Subtrahend How can we know if the result is incorrect? How to fix the problem?
37Example Borrow11100 (M) Minuend (N) Subtrahend Difference10101 Correct Diff If no borrow, then result is non-negative (minuend >= subtrahend). Since there is borrow, result must be negative. The result must be corrected to a negative number. 19 – 30 = -11 Procedure?
38 Algorithm: Subtraction of two n-digit Numbers M-N can be done as follows 1. Subtract N from M If no borrow, then M N and result is OK ! Otherwise, N > M so result must be subtracted from 2 n and a minus sign should be appended 2. NOTE: Subtraction of a binary number from 2 n to obtain an n-digit result is called 2’s complement 3. Circuit?
39 Adder/Subtractor Circuit!! Adder/Subtractor Circuit!! Binary Adder Binary Subtractor EXPENSIVE!!
40 How to get rid of Subtraction Operation? Use complements of numbers to replace the subtraction operation with addition only. Any Idea?
41 Subtraction of Unsigned Numbers Using Complements 1. M – N Equivalent to M + (2’s complement of N) 2. Add (2’s complement of N) to M This is M + (2 n – N) = M – N + 2 n Notice we are using addition to achieve subtraction. will generate 3. If M N, will generate carry! Result is correct Simply discard carry Result is positive M - N no end carry 4. If M < N, no end carry will be generated! Make Correction Take 2’s complement of result Place minus sign in front
42 Example o X = minus Y = o Notice that X > Y o The 2’s complement of Y= is obtained first by getting the 1’s complement and then adding 1 ( ) X ’s comp Y Sum
43 Example 2 Y = minus X = Notice Y < X No end carry Answer: - (2’s complement of Sum) Y ’s comp X Sum We said numbers are unsigned. What does this mean?
44Adder-Subtractor I. By using 2’s complement approach we were able to get rid of the design of a subtractor. II. Need only adder and complementer for input to subtract III. Need selective complementer to make negative output back from 2’s complement
45 Selective 1’s Complementer? Control When X = 0 we transfer Y to output When X = 1 we complement Y
46Design Inverts each bit of B if S is 1 Adds 1 to make 2’s complement S low for add, high for subtract
47 Negative Numbers CC omputers Represent Information in ‘0’s and ‘1’s ●‘●‘+’ and ‘−’ signs have to be represented in ‘0’s and ‘1’s 33 Systems ●S●Signed Magnitude ●1●1’s Complement ●2●2’s Complement All three use the left-most bit to represent the sign: ♦‘♦‘ 0’ positive ♦‘♦‘ 1’ negative
48 Signed Binary Numbers First review signed representations Signed magnitude Left bit is sign, 0 positive, 1 negative Other bits are number -9 2’s complement 1’s complement
49 Signed Magnitude Representation MM agnitude is magnitude, does not change with sign (+3) 10 ( ) 2 (−3) 10 ( ) 2 SignMagnitude SMagnitude (Binary)
50 1’s Complement Representation PP ositive numbers are represented in “Binary” NN egative numbers are represented in “1’s Comp.” (+3) 10 (0 011) 2 (−3) 10 (1 100) 2 TT here are 2 representations for ‘0’!!!!!! (+0) 10 (0 000) 2 (−0) 10 (1 111) 2 0Magnitude (Binary) 1Code (1’s Comp.)
51 1’s Complement Range 4-Bit Representation 2 4 = 16 Combinations − 7 ≤ Number ≤ + 7 − ≤ Number ≤ +2 3 − 1 n-Bit Representation −2 n−1 +1 ≤ Number ≤ +2 n−1 − 1 Decimal1’s Comp − 01 1 − − − − − 51 0 − −
52 2’s Complement Representation PP ositive numbers are represented in “Binary” NN egative numbers are represented in “2’s Comp.” (+3) 10 (0 011) 2 (−3) 10 (1 101) 2 TT here is 1 representation for ‘0’ (+0) 10 (0 000) 2 (−0) 10 (0 000) 2 0Magnitude (Binary) 1Code (2’s Comp.) 1’s Comp
53 2’s Complement Range 4-Bit Representation 2 4 = 16 Combinations − 8 ≤ Number ≤ + 7 −2 3 ≤ Number ≤ − 1 n-Bit Representation −2 n−1 ≤ Number ≤ + 2 n−1 − 1 Decimal2’s Comp − 11 1 − − − − − 61 0 − −
54 Convert 2’s Complement to Decimal bit index bit weighting Example Decimal 0x-2 7 1x2 6 0x2 5 1x2 4 0x2 3 0x2 2 1x2 1 0x = 82 bit index bit weighting Example Decimal 1 x-2 7 0x2 6 1x2 5 0x2 4 1x2 3 1x2 2 1x2 1 0x = -82
55 Number Representations 4-Bit Example Unsigned Binary Signed Magnitude 1’s Comp.2’s Comp. Range0 ≤ N ≤ 15-7 ≤ N ≤ ≤ N ≤ +7 Positive Binary Negative X Binary1’s Comp.2’s Comp
56 Example in 8-bit byte Represent +9 in different ways Signed magnitude ’s Complement ’s Complement Represent -9 in different ways Signed magnitude ’s Complement ’s Complement The Same!
57Observations All positive numbers are the same 1’s Comp and Signed Mag have two zeros 2’s Comp has more negative than positive All negative numbers have 1 in high-order bit
58 Advantages/Disadvantages Signed magnitude has problem that we need to correct after subtraction One’s complement has a positive and negative zero Two’s complement is most popular i.e arithmetic operations are easy
59 Signed Magnitude Representation MM agnitude is magnitude, does not change with sign (+3) 10 ( ) 2 (−3) 10 ( ) 2 CC an’t include the sign bit in ‘Addition’ (+3) (−3) (−6) 10 SignMagnitude SMagnitude (Binary)
60 Signed Magnitude Representation The signed-magnitude system is used in ordinary arithmetic, but is awkward when employed in computer arithmetic (Why?) 1. We have to separately handle the sign 2. Perform the correction if necessary!! Therefore the signed complement (1’s complement and 2’s complement number representations) is normally used.
61 Signed Magnitude Arithmetic Complex Rules!! The addition of two numbers M+N in the sign magnitude system follows the rules of ordinary arithmetic: signs are the same If the signs are the same, we add the two magnitudes and give the sum the sign of M. signs are different If the signs are different, we subtract the magnitude of N from the magnitude of M. end borrow The absence or presence of an end borrow then determines: The sign of the result. Whether or not a correction is performed. Example: ( ) + ( ) – = End borrow of 1 occurs, M < N!! Sign of result should be that of N, Also correct result by taking the 2’s complement of result
62 Binary Subtraction Using 1’s Comp. Addition CC hange “Subtraction” to “Addition” II f “Carry” = 1 then add it to the LSB, and the result is positive (in Binary) II f “Carry” = 0 then the result is negative (in 1’s Comp.) (5) 10 – (1) 10 (+5) 10 + (-1) (5) 10 – (6) 10 (+5) 10 + (-6) − 1 1
63 Two’s Complement To Add: Easy Easy on any combination of positive and negative numbers To subtract: Also easy Also easy! 2’s complement Take 2’s complement of subtrahend Add Add This performs A + ( -B), same as A – B
64 Binary Subtraction Using 2’s Comp. Addition CC hange “Subtraction” to “Addition” II f “Carry” = 1 ignore it, and the result is positive (in Binary) II f “Carry” = 0 then the result is negative (in 2’s Comp.) (5) 10 – (1) 10 (+5) 10 + (-1) (5) 10 – (6) 10 (+5) 10 + (-6) − 1
65 Examples from Book Addition (+6) + 13 (-6) + 13 (+6) + (- 13) (-6) + (-13) Subtraction (-6) - (-13) (+6) - (-13) The numbers below should be in 2’s comp representation
66 Addition of Two Positive Numbers Addition (+6) + 13 = +19 If a carry out appears it should be discarded.
67 Addition of : a Positive and Negative Numbers Addition (-6) + 13 = (this is 2’s comp of +6) The carry out was discarded
68 Subtraction of Two Numbers subtraction The subtraction of two signed binary numbers (when negative numbers are in 2’s complement form) can be accomplished as follows: 1. Take the 2’s complement of the subtrahend (including the sign bit) 2. Add it to the minuend. 3. A Carry out of the sign bit position is discarded.
69 Subtraction of Two Numbers Subtraction (+6) – (+13) = (2’s comp) What is ? Take its 2’s complement=> The magnitude is 7 So it must be -7
70 Circuit for 2’s complement Numbers No Correction is needed if the signed numbers are in 2’s complement representation
71 Sign Extension Sign extension is the operation, in computer arithmetic, of increasing the number of bits of a binary number while preserving the number’s sign (positive/negative) and value. This is done by appending digits to the most significant side of the number Examples: 2’s complement (6-bits 8-bits) 2’s complement (5-bits 8-bits):
72 Overflow sufficient In order to obtain a correct answer when adding and subtracting, we must ensure that the result has a sufficient number of bits to accommodate the sum. If we start with two n-bit numbers and we end up with a number that is n+1 bits, we say an overflow has occurred.
73 Overflow Two cases of overflow for addition of signed numbers Two large positive numbers overflow into sign bit Not enough room for result Two large negative numbers added Same – not enough bits Carry out can be OK
74Examples 8-bit registers Two signed numbers +70 and +80 are stored in 8-bit registers. The range The range of binary numbers, expressed in decimal, that each register can accommodate is from +127 to Since the sum of the two stored numbers is 150, it exceeds the capacity of an 8-bit register. The same applies for -70 and -80.
75 Overflow Detection Carries: 0 1 Carries: The addition of +70 and +80 resulted in a negative number! 2. The addition of -70 and -80 also resulted in an incorrect value which is positive number! 3. An overflow condition can be detected by observing the carry into the sign bit position and the carry out of the sign bit position. 4. If the the carry in and carry out of the sign bit are not equal an overflow has occurred.
76 Circuit for Overflow Detection Condition is that either C n-1 or C n is high, but not both
77 Recall: Arithmetic -- multiplication X 101
78 Multiplier Multiply by doing single-bit multiplies and shifts Combinational circuit to accomplish this?
79 Combinational Multiplier AND computes A 0 B 0 Half adder computes sum. Will need FA for larger multiplier.
80 Larger Multiplier: Resources For J multiplier bits and K multiplicand bits we need o J x K AND gates o (J-1) K-bit adders to produce a product of J+K bits.
81 Larger Multiplier A k=4-bit by j=3-bit Binary Multiplier. J = 3 K = 4 Jxk = 12 AND Gates (J-1) Adders Of k bits each
83 Carry Look ahead Adder Note that add itself just 2 level Idea is to separate carry from adder function Then make carry approx 2-level all way across larger adder
84 One-bit Subtractor Inputs: Borrow in, minuend subtrahend Outputs: Difference, borrow out Truth Table? 1-bit sub MS B out D B in
85 Correcting Result Borrow11100 Minuend Subtrahend Difference
86 Correcting Result If M is minuend and N subtrahend of numbers length n, difference was (2 n + M) – N What we want is magnitude of N-M with minus sign in front We can get the correct result by subtracting previous result from 2 n N - M = 2 n – (M – N + 2 n )
87 Interpretation of the incorrect result Borrow11100 Minuend Subtrahend Difference M N 2525 (2 n + M) – N
88 Correcting Result What, mathematically, does it mean to borrow? It means that M < N If borrowing at digit i-1 you are adding 2 i
89 Designs Aren’t Like This That’s why people use complemented interpretation for signed numbers 2’s complement 1’s complement
90 2’s Complement The 2’s complement of is The circuit that performs this is a complementer
91 1’s Complement Given: binary number N with n digits 1’s complement defined as (2 n – 1) - N Note that (2 n – 1) is number with n digits, all of them 1 For n = 4, (2 n – 1) = 1111
92 2’s Complement Given: binary number N with n digits 2’s complement defined as 2 n – N for N 0 0 for N = 0 Exception is so that the result will always have n bits 2’s complement is just a 1 added to 1’s complement
93 Still Remember? Unsigned Arithmetic Subtraction No borrows Borrows results in a borrow Subtrahend Minuend
94 Four-bit Carry Look Ahead Adder function separated from carry Notice adder has A, B, C in and S out, as well as G,P out. Reference
ENG241/Digital Design95 BCD Addition 95