arithmetic circuits i. 2 iterative combinational circuits like a hierachy, except functional blocks...
TRANSCRIPT
3
Adders
• Great example of Iterative design• Design a 1-bit adder circuit, then
expand to n-bit adder• Look at
♦ Half adder – which is a 2-bit adder♦ Inputs are bits to be added
• Outputs: result and possible carry
♦ Full adder – includes carry in, really a 3-bit adder
8
Using two half Adders to Build a Full Adder
• Full adder functions
• Half adder functionsS = X YC = XY
10
Ripple-Carry Adder
• Straightforward – connect full adders
• C4 : Chain carry-out to carry-in of FA of bits A4 & B4
♦ C0 in case this is part of larger chain
♦ otherwise just set to zero
11
Hierarchical 4-Bit Adder
• We can easily use hierarchy here
• Design half adder• Use in full adder• Use full adder in 4-bit adder
12
Binary Subtraction
• Example: ♦ Let M = (19)10 ; N = (30)10 ; How is M-N
computed?• In decimal notation: (19)10 – (30)10 = - (11)10
• In binary: (10011)2 - (11110)2 = - (01011)2
13
Using 2’s & 1’s Complement Representation of Signed Numbers
• People use complemented interpretation for signed numbers♦ 1’s complement♦ 2’s complement
14
1’s Complement
• Given: binary number N with n digits1’s complement is defined as(2n – 1) - N
• Note that (2n – 1) is a number with n bits, all of them 1♦ For n = 4, (2n – 1) = (15)10 = 1111
15
Example: Find 1’s Complement of N =101 1001
• Notice that 1’s complement is complement of each bit
n= 7; 2n - 1 1 1 1 1 1 1 1
- N 1 0 1 1 0 0 1
1’s Compl. 0 1 0 0 1 1 0
•1’s Complement of N is (2n – 1) - N
16
2’s Complement
• Given: binary number N with n digits2’s complement defined as
2n – N for N 00 for N = 0
• Note that since 1’s complement is
(2n – 1) – N
♦ 2’s complement is just a 1 added to 1’s complement
17
Example: Find 2’s Complement of N =101 1001
• To find 2’s complement of N♦ Find is 1’s complement of N then add 1
N 1 0 1 1 0 0 1
1’s Compl. 0 1 0 0 1 1 0
+1 0 0 0 0 0 0 1
2’s Compl. 0 1 0 0 1 1 1
• 2’s Complement of N is 2n - N
19
Subtraction Using 2s Complement:Compute M-N
1. Add 2’s complement of N to M♦ M + (2n – N) = { M – N + 2n } = F
2. If M N, this addition will generate a carry, 2n
• Discard carry; the result gives the answer of M-N which is positive
3. If M < N, no carry; M-N is negative; the answer is –(N-M). To compute the answer:
• Take 2’s complement of F• 2n – F = 2n - { M – N + 2n } = N-M
• Place minus sign in front { - (N-M) }; This is the answer
4. Hence to compute (M-N): do 1 & 2 if M N; OR 1 & 3 If M < N
20
Compute M-N;Example 1: M > N• M = (84)10= (101 0100)2
• N = (67)10 = (100 0011)2;
♦ 2’s comp N = ( 011 1100 ) + 1 = 011 1101M 1 0 1 0 1 0 0
+ 2’s comp N 0 1 1 1 1 0 1
Sum 1 0 0 1 0 0 0 1
M > N; Carry generated; Discard carry; Result is positive M - N
1710
21
Compute M-N; Example 2: M < N;M = 6710 = 100 0011; N = 8410 = 101 0100
• M = 100 0011 minus N = 101 0100
• No end carry♦ Answer: - (2’s complement of Sum)
• Answer: - 0010001
M 1 0 0 0 0 1 1
+ 2’s comp N 0 1 0 1 1 0 0
Sum 1 1 0 1 1 1 1
We said numbers are unsigned. What does this mean?How is -1710 represented?
22
Subtractor
• Compute M-N♦ Add 2’s complement of N to M:
M + (2n – N) • If M >= N, need only “one adder”
and a “complementer of N” to do the subtraction.
• If M < N, we also need to take 2’s complement of adder’s output to produce magnitude of result2n -{ M + (2n – N)} = N-M
23
Adder-Subtractor Design: (A+B)OR (A-B)
• Output is result if A >= B; Discard carry• Output is 2’s complement of result if B > A
S is low for add,high for subtract
Inverts each bit of B if S is 1
Fig. 4-7
Add 1 tomake 2’s complement
24
Signed Binary
1. Signed magnitude♦ Left bit is sign, 0 positive, 1 negative♦ Other bits are number
2. 2’s complement3. 1’s complement
25
Example in 8-bit byte
• Represent -910 = -(0000 1001) in different ways
“Signed magnitude” of - (0000 1001) is1000 1001
“Signed 1’s Complement” of - (0000 1001) is 1111 0110
“Signed 2’s Complement” of - (0000 1001) is1111 0111
26
Observations (assume 4-bit numbers)
• 1’s C and Signed Magnitude have two zeros
• 2’s C has more negative numbers than positive
• All negative numbers have 1 in highest-order bit
27
Advantages/Disadvantages
• Signed magnitude & One’s complement has a positive and negative zero
• Two’s complement is most popular♦ Arithmetic operations easy
28
Two’s Complement
• Addition easy on any combination of positive and negative numbers
• To compute A - B♦ Take 2’s complement of subtrahend B
to produce -B♦ Add to A
•This performs A + ( -B), same as A – B
29
Examples• Assume we use 2’s complement
representation of signed numbers• Store each number in 8 bits 6 = (0000 0110) ; 13 = (0000 1101)-6 = (1111 1010) ; -13 = (1111 0011)
• Addition♦ 6 + 13♦ -6 + 13♦ 6 + (- 13)♦ (-6) + (-13)
• Subtraction♦ -6 - (-13)♦ 6 - (- 13)
30
Overflow
• Overflow means that result can not be represented with the number of bits used
• Two cases of overflow for addition of signed numbers♦ Two large positive numbers
overflow into sign bit•Not enough bits for result
♦ Two large negative numbers added•Same – Not enough bits for result
31
Examples:Work on Board
Assume
1- We use “2’s complement representation”
of signed numbers
2- Store each number in 4 bits : b3 b2 b1 b0
32
Example 1
• 7 + 7
• Overflow: can not represent 14 using only 4 bits
• Generates NO CARRY, C4 = 0
36
Overflow Detection• Condition for overflow:
♦ either Cn-1 or Cn is high, but not both
♦ 7 + 7; only Cn-1 (C3) is high ; overflow♦ -7 - 7; only Cn (C4) is high ; overflow♦ 4 + 4; only Cn-1 (C3) is high ; overflow♦ 7 – 7; BOTH Cn-1 & Cn (C4 & C3) are high;
no overflow
---------------------------------------------------------Fig. 5-9 Overflow Detection for Addition and SubtractionFig. 4-8