1
EE 457 Unit 2 Fixed Point Systems and Arithmetic 2 Unsigned 2s - - PowerPoint PPT Presentation
EE 457 Unit 2 Fixed Point Systems and Arithmetic 2 Unsigned 2s - - PowerPoint PPT Presentation
1 EE 457 Unit 2 Fixed Point Systems and Arithmetic 2 Unsigned 2s Complement Sign and Zero Extension Hexadecimal Representation SIGNED AND UNSIGNED SYSTEMS 3 Signed Systems Several systems have been used 2s complement system
2
SIGNED AND UNSIGNED SYSTEMS
Unsigned 2’s Complement Sign and Zero Extension Hexadecimal Representation
3
Signed Systems
- Several systems have been used
– 2’s complement system – 1’s complement system – Sign and magnitude
4
Unsigned and Signed Variables
- Unsigned variables use unsigned binary (normal
power-of-2 place values) to represent numbers
- Signed variables use the 2’s complement system
(Neg. MSB weight) to represent numbers
128 64 32 16 8 4 2 1 1 1 1 1 = +147
- 128
64 32 16 8 4 2 1 1 1 1 1 = -109
5
2’s Complement System
- MSB has negative weight
- MSB determines sign of the number
– 1 = negative – 0 = positive
- To take the negative of a number
(e.g. -7 => +7 or +2 => -2), requires taking the complement
– 2’s complement of a # is found by flipping bits and adding 1
1001 0110 + 1
0111
x = -7 Bit flip (1’s comp.) Add 1
- x = -(-7) = +7
6
Zero and Sign Extension
2’s complement = Sign Extension (Replicate sign bit): Unsigned = Zero Extension (Always add leading 0’s): 111011 = 00111011 011010 = 00011010 110011 = 11110011 pos. neg.
Increase a 6-bit number to 8-bit number by zero extending Sign bit is just repeated as many times as necessary
- Extension is the process of increasing the number of bits used
to represent a number without changing its value
7
Zero and Sign Truncation
- Truncation is the process of decreasing the number of bits used
to represent a number without changing its value 2’s complement = Sign Truncation (Remove copies of sign bit): Unsigned = Zero Truncation (Remove leading 0’s): 00111011 = 111011 00011010 = 011010 11110011 = 10011 pos. neg.
Decrease an 8-bit number to 6-bit number by truncating 0’s. Can’t remove a ‘1’ because value is changed Any copies of the MSB can be removed without changing the numbers value. Be careful not to change the sign by cutting off ALL the sign bits.
8
Arithmetic & Sign
- You learned the addition (carry-method) and
subtraction (borrow-method) algorithms in grade school
- Consider A + B…do you definitely use the addition
algorithm?
– Not if A=5, B=(-2)…5 + (-2) = 5 – 2 = 3 – What if A=(2), B=(-5)? – Can’t perform 2-5 – Flip operands and keep sign of larger
- 5 – 2 = 3 => Apply sign of larger mag. operand => -3
- Human add/sub algorithm depends on sign!!
9
Unsigned and Signed Arithmetic
- Addition/subtraction process is the same for
both unsigned and signed numbers
– Add columns right to left – Drop any final carry out
- This is the KEY reason we use 2’s complement
system to represent signed numbers
- Examples:
1001 + 0011 1100
1 1
(9) (3) (12) (-7) (3) (-4)
If unsigned If signed
10
Unsigned and Signed Subtraction
- Subtraction process is the same for both
unsigned and signed numbers
– Convert A – B to A + Comp. of B – Drop any final carry out
- Examples:
(12) (2) (-4) (2)
If unsigned If signed
1100
- 0010
11_1_ 1100 1101 + 1 1010
1’s comp. of B Add 1 A If unsigned If signed
(10) (-6)
11
Overflow
- Overflow occurs when the result of an
arithmetic operation is too large to be represented with the given number of bits
– Unsigned overflow (C) occurs when adding or subtracting unsigned numbers – Signed (2’s complement overflow) overflow (V)
- ccurs when adding or subtracting 2’s
complement numbers
12
Unsigned Overflow
0000 0001 0010 0011 0100 0101 0110 0111 1000 1111 1110 1101 1100 1011 1010 1001 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15
Overflow occurs when you cross this discontinuity
10 Plus 7
10 + 7 = 17
With 4-bit unsigned numbers we can only represent 0 – 15. Thus, we say overflow has occurred.
4 - 6 = 14
13
2’s Complement Overflow
0000 0001 0010 0011 0100 0101 0110 0111 1000 1111 1110 1101 1100 1011 1010 1001 +1 +2 +3 +4 +5 +6 +7
- 8
- 7
- 6
- 5
- 4
- 3
- 2
- 1
Overflow occurs when you cross this discontinuity
- 6 + -4 = -10
With 4-bit 2’s complement numbers we can only represent
- 8 to +7. Thus, we say overflow
has occurred.
5 + 7 = +12
14
Testing for Overflow
- Most fundamental test
– Check if answer is wrong (i.e. Positive + Positive yields a negative)
- Unsigned overflow (C) test
– If carry-out of final position equals ‘1’
- Signed (2’s complement) overflow (V) test
– Only occurs if two positives are added and result is negative or two negatives are added and result is positive – Alternate test: See following slides
15
Alternate Signed Overflow Test
- Check if Cin & Cout of MSB column are different
A & B A3 B3 S3 C3 C4 V Both Positive 1 1 1 One Positive & One Negative 1 1 1 1 1 1 1 1 Both Negative 1 1 1 1 1 1 1
16
Overflow in Addition
- Overflow occurs when the result of the
addition cannot be represented with the given number of bits.
- Tests for overflow:
– Unsigned: if Cout = 1 – Signed: if p + p = n or n + n = p
1101 + 0100 0001
1 1
(13) (4) (17) (-3) (4) (+1)
If unsigned If signed Overflow Cout = 1 No Overflow n + p
0110 + 0101 1011
1
(6) (5) (11) (6) (5) (-5)
If unsigned If signed No Overflow Cout = 0 Overflow p + p = n
17
Overflow in Subtraction
- Overflow occurs when the result of the subtraction
cannot be represented with the given number of bits.
- Tests for overflow:
– Unsigned: if Cout = 0 – Signed: if addition is p + p = n or n + n = p (7) (8) (-1) (7) (-8) (15)
If unsigned If signed
0111
- 1000
0111_ 0111 0111 + 1 1111
1’s comp. of B Add 1 A If unsigned Overflow Cout = 0 If signed Overflow p + p = n
(15) (-1)
Desired Results
18
Addition – Full Adders
- Use 1 Full Adder for each column of addition
0110 + 0111
Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout
19
Addition – Full Adders
- Connect bits of top number to X inputs
0110 + 0111
Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout 1 1
20
Addition – Full Adders
- Connect bits of bottom number to Y inputs
0110 + 0111 = X = Y
Full Adder X Y Cin S Cout 1 Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout 1 1 1 1
21
Addition – Full Adders
- Be sure to connect first Cin to 0
0110 + 0111 = X = Y
Full Adder X Y Cin S Cout 1 Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout 1 1 1 1
22
Addition – Full Adders
- Use 1 Full Adder for each column of addition
0110 + 0111 1101 = X = Y 01100
Full Adder X Y Cin S Cout 1 1 Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout 1 1 1 1 1 1 1 1
23
Performing Subtraction w/ Adders
- To subtract
– Flip bits of Y – Add 1
0101
- 0011
1101 = X = Y
Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout
0101 + 1100 1 0010
24
Performing Subtraction w/ Adders
- To subtract
– Flip bits of Y – Add 1
0101
- 0011
1101 = X = Y
Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout
0101 + 1100 1 0010
1 1 1 1 1 1
25
Performing Subtraction w/ Adders
- To subtract
– Flip bits of Y – Add 1
0101
- 0011
1101 = X = Y
Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout
0101 + 1100 1 0010
1 1 1 1 1 1 1
26
Performing Subtraction w/ Adders
- To subtract
– Flip bits of Y – Add 1
0101
- 0011
1101 = X = Y
Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout Full Adder X Y Cin S Cout
0101 + 1100 1 0010
1 1 1 1 1 1 1 1 1 1 1
27
XOR Gate Review
XOR Z X Y X Y Z 0 0 0 0 1 1 1 0 1 1 1 0
Y X Z
True if an odd # of inputs are true 2 input case: True if inputs are different
28
XOR Conditional Inverter
- If one input to an XOR gate is 0,
the other input is passed
- If one input to an XOR gate is 1,
the other input is inverted
- Use one input as a control input
which can conditionally pass or invert the other input
X Y Z 0 0 0 0 1 1 1 0 1 1 1 0 Y Y
29
Adder/Subtractor
- Using XOR gates
before one set of adder inputs we can
– Selectively pass or invert Y – Add an extra ‘1’ via the Carry-in
- If SUB/~ADD=0,
– Z = X+Y
- If SUB/~ADD=1,
– Z = X-Y
30
Adder/Subtractor
- Exercise: Add
appropriate logic to produce
– C (unsigned overflow) – V (signed overflow) flags
31
ALU Design
Complete the ALU design given the function table below
OP[2:0] Z 000 X+Y 001 X-Y 011 SLT: Z=1, if X<Y Z=0, other 100 AND 110 OR Others Z = und.
32
NON-REQUIRED MATERIAL
33
Hexadecimal Representation
- Since values in modern computers are many
bits, we use hexadecimal as a shorthand notation (4 bits = 1 hex digit)
– 11010010 = D2 hex – 0111011011001011 = 76CB hex
- To interpret the value of a hex number, you
must know what underlying binary system is assumed (unsigned, 2’s comp. etc.)
34
Translating Hexadecimal
- Hex place values (162, 161, 160) can ONLY be used if
the number is positive.
- If hex represents unsigned binary simply apply hex
place values
– B2 hex = 11*161 + 2*160 = 17810
- If hex represents signed value (2’s comp.)
– First determine the sign to be pos. or neg.
- Convert the MS-hex digit to binary to determine the MSB (e.g. for
B2 hex, B=1011 so since the MSB=1, B2 is neg.)
- In general, hex values starting 0-7 = pos. / 8-F = neg.
– If pos., apply hex place values (as if it were unsigned) – If neg., take the 16’s complement and apply hex place values to find the neg. number’s magnitude
35
Taking the 16’s Complement
- Taking the 2’s complement of a binary number yields
its negative and is accomplished by finding the 1’s complement (bit flip) and adding 1
- Taking the 16’s complement of a hex number yields
its negative and is accomplished by finding the 15’s complement and adding 1
– 15’s complement is found by subtracting each digit of the hex number from F16
FF
- B2
4D + 1
4E
Subtract each digit from F 15’s comp. of B2 Add 1 16’s comp. of B2 Original value B2: 16’s comp. of B2:
36
Translating Hexadecimal
- Given 6C hex
– If it is unsigned, apply hex place values
- 6C hex = 6*161 + 12*160 = 10810
–If it is signed…
- Determine the sign by looking at MSD
– 0-7 hex has a 0 in the MSB [i.e. positive] – 8-F hex has a 1 in the MSB [i.e. negative] – Thus, 6C (start with 6 which has a 0 in the MSB is positive)
- Since it is positive, apply hex place values
– 6C hex = 6*161 + 12*160 = 10810
37
Translating Hexadecimal
- Given FE hex
– If it is unsigned, apply hex place values
- FE hex = 15*161 + 14*160 = 25410
–If it is signed…
- Determine sign => Negative
- Since it is negative, take 16’s complement and then
apply place values – 16’s complement of FE = 01 + 1 = 02 and apply place values = 2 – Add in sign => -2 = FE hex
38
Finding the Value of Hex Numbers
- B2 hex representing a signed (2’s comp.) value
– Step 1: Determine the sign: Neg. – Step 2: Take the 16’s comp. to find magnitude FF - B2 + 1 = 4E hex – Step 3: Apply hex place values (4E16 = +7810) – Step 4: Final value: B2 hex = -7810
- 7C hex representing a signed (2’s comp.) value
– Step 1: Determine the sign: Pos. – Step 2: Apply hex place values (7C16 = +12410)
- 82 hex representing an unsigned value
– Step 1: Apply hex place values (8216 = +13010)
39
Hex Addition and Overflow
- Same rules as in binary
– Add left to right – Drop any carry (carry occurs when sum > F16)
- Same addition overflow rules
– Unsigned: Check if final Cout = 1 – Signed: Check signs of inputs and result 7AC5 + C18A 3C4F
1 1
If unsigned If signed Overflow Cout = 1 No Overflow p + n
6C12 + 549F C0B1
1
If unsigned If signed No Overflow Cout = 0 Overflow p + p = n
1
40
Hex Subtraction and Overflow
- Same rules as in binary
– Convert A – B to A + Comp. of B – Drop any final carry out
- Same subtraction overflow rules
– Unsigned: Check if final Cout = 0 – Signed: Check signs of addition inputs and result B1ED
- 76FE
B1ED 8901 + 1 3AEF
If unsigned No Overflow Cout = 1 If signed Overflow n + n = p
1
0001
- 0002
0001 FFFD + 1 FFFF
If unsigned Overflow Cout = 0 If signed No Overflow p + n
41
Credits
- These slides were derived from Gandhi