Beruflich Dokumente
Kultur Dokumente
ELEC2200-002 Lecture 2
ELEC2200-002 Lecture 2
Positive Integers
Decimal system: made of 10 digits, {0,1,2, . . . , 9}
41 = 4101 + 1100
255 = 2102 + 5101 + 5100
Binary system: made of two digits, {0,1}
00101001
= 027 + 026 + 125 + 024
+123 + 022 + 021 + 120
= 32 + 8 +1 = 41
11111111 = 255, largest number with 8
binary digits, 28-1
Fall 2012, Aug 20 . . .
ELEC2200-002 Lecture 2
Base or Radix
For decimal system, 10 is called the base or
radix.
Decimal 41 is also written as 4110 or 41ten
Base (radix) for binary system is 2.
Thus, 41ten = 1010012 or 101001two
Also,
111ten
= 1101111two
and
111two
= 7ten
ELEC2200-002 Lecture 2
Example:
+18ten = 00010010two
18ten = 10010010two
ELEC2200-002 Lecture 2
ELEC2200-002 Lecture 2
-4
0000
0
0100
4
1000
8
1100
12
10000 10100
16
20
4-bit numbers
ELEC2200-002 Lecture 2
4-bit Universe
1111
Modulo-16
(4-bit)
universe
15
0000
16/0
0001
1100 12
4
8
0100
1000
0000
1111
15 0
-0
0001
1100 12 -3
4
-7
8
1000
0100
7
0111
ELEC2200-002 Lecture 2
0001
1100 12 -3
4
-7
8
1000
Decimal
magnitude
0100
7
0111
Binary number
Positive Negative
0000
1111
0001
1110
0010
1101
0011
1100
0100
1011
0101
1010
0110
1001
0111
1000
ELEC2200-002 Lecture 2
0001
1100 12 -4
Subtract 1
on this side
4
-8
8
1000
0100
7
0111
Binary number
Decimal
magnitude
Positive
0000
0001
1111
0010
1110
0011
1101
0100
1100
0101
1011
0110
1010
0111
1001
ELEC2200-002 Lecture 2
Negative
1000
10
ELEC2200-002 Lecture 2
11
2s-Complement Integers
Why not 1s-complement? Dont like two zeros.
Negation rule:
Subtract 1 and then invert bits, or
Invert bits and add 1
Some properties:
Only one representation for 0
Exactly as many positive numbers as negative numbers
Slight asymmetry there is one negative number with no
positive counterpart
ELEC2200-002 Lecture 2
12
ELEC2200-002 Lecture 2
13
Three Systems (n = 4)
1111
7
2
1010
0000
0
0010
7
0111
1000
1010 = 2
Signed magnitude
Fall 2012, Aug 20 . . .
1111
0
0000
1111
5 7
1010
1000
10000
0000
0
0111
1010 = 5
6
1010
1000
6
0111
1010 = 6
14
Three Representations
Sign-magnitude
1s complement
2s complement
000 = +0
001 = +1
010 = +2
011 = +3
100 = - 0
101 = - 1
110 = - 2
111 = - 3
000 = +0
001 = +1
010 = +2
011 = +3
100 = - 3
101 = - 2
110 = - 1
111 = - 0
000 = +0
001 = +1
010 = +2
011 = +3
100 = - 4
101 = - 3
110 = - 2
111 = - 1
(Preferred)
ELEC2200-002 Lecture 2
15
2s Complement Numbers (n = 3)
0
000
subtraction
addition
-1 111
001 +1
s
ber
um
en
itiv
s
Pos
ber
num
a
Neg
tive
-2 110
010
+2
011 +3
-3 101
100
ow
l
f
r
Ove
Negation
-4
Fall 2012, Aug 20 . . .
ELEC2200-002 Lecture 2
16
ELEC2200-002 Lecture 2
17
Summary
For a given number (n) of digits we have a
finite set of integers. For example, there are
103 = 1,000 decimal integers and 2 3 = 8 binary
integers in 3-digit representations.
We divide the finite set of integers [0, r n 1],
where radix r = 10 or 2, into two equal parts
representing positive and negative numbers.
Positive and negative numbers of equal
magnitudes are complements of each other: x
+ complement (x) = 0.
Fall 2012, Aug 20 . . .
ELEC2200-002 Lecture 2
18
Binary integers:
2s complement: x = Complement (x) = 2n x
1s complement: x = Complement (x) = 2n 1 x
ELEC2200-002 Lecture 2
19
Understanding Complement
Complement means something that
completes:
e.g., X + complement (X) = Whole.
Complement also means opposite,
e.g., complementary colors are placed
opposite in the primary color chart.
Complementary numbers are like electric
charges. Positive and negative charges
of equal magnitudes annihilate each
other.
Fall 2012, Aug 20 . . .
ELEC2200-002 Lecture 2
20
2s-Complement Numbers
Infinite - . . . -1
universe
of integers
Finite
Universe of
3-digit
Decimal
numbers
999
000
0
001
1
1000
000
501
010
2
011
3
111
1000
000
001
001
499
011
101
500
Fall 2012, Aug 20 . . .
100
4
101
5...
Finite
Universe
of 3-bit
binary
numbers
100
ELEC2200-002 Lecture 2
21
Examples of Complements
Decimal integers (r = 10, n = 3):
10s complement: 50 = Compl (50) = 103 50 =
950; 50 + 950 = 1,000 = 0 (in 3 digit representation)
9s complement: 50 = Compl (50) = 10n 1 50 =
949; 50 + 949 = 999 = 0 (in 9s complement rep.)
ELEC2200-002 Lecture 2
22
2s-Complement to Decimal
Conversion
n-2
8
1
4
1
2
0
1
1
1
ELEC2200-002 Lecture 2
23
ELEC2200-002 Lecture 2
24
Addition
Adding bits:
0+0=
0
0+1=
1
1+0=
1
1 + 1 = (1) 0
carry
Adding integers:
0 0 0...... 0
0 0 0...... 0
1
1
1
1
1 two =
0 two =
7ten
6ten
ELEC2200-002 Lecture 2
25
Subtraction
Direct subtraction
0 0 0......0
0 0 0......0
1
1
1
1
1 two =
0 two =
7ten
6ten
= 0 0 0...... 0
1two =
1ten
1 1 1......1
0 0 0......0
1 1 1......1
1
1
0
0
1
1
1 two = 7ten
0 two = 6ten
ELEC2200-002 Lecture 2
1ten
26
Overflow: An Error
Examples: Addition of 3-bit integers (range - 4 to +3)
-2-3 = -5
110 = -2
+ 101 = -3
= 1011 = 3 (error)
000
0
111
-1
1
+
2 010
110 -2
3+2 = 5
011 = 3
010 = 2
= 101 = -3 (error)
101
-3
-4
001
011
100 Overflow
crossing
ELEC2200-002 Lecture 2
27
Decrease
1111
1110
1101
1100
0000
1011
1010
1001 1000
Fall 2012, Aug 20 . . .
0010
Increase
0011 0100
0001
0010
Increase
Infinite - . . .1111
universe
of integers
No overflow
Decrease
0000
0001
0011
0100
0101
0110
0111
Forbidden fence
ELEC2200-002 Lecture 2
0101 . . .
Finite
Universe
of 4-bit
binary
integers
28
s = a+b
Decimal
Binary
00
01
01
10
CARRY
Fall 2012, Aug 20 . . .
ELEC2200-002 Lecture 2
SUM
29
0
1
0
1
half_sum carry_out
h_s(a, b) c_o(a, b)
a
0
0
1
0
b
1
0
0
1
ELEC2200-002 Lecture 2
XOR
half_sum
HA
AND
carry_out
30
s=a+b+c
Decimal
Binary
00
01
01
10
01
10
10
11
CARRY
ELEC2200-002 Lecture 2
SUM
31
HA
AND
FA
h_s
(a, b)
XOR
c_o
(a, b)
HA
AND
ELEC2200-002 Lecture 2
h_s
(h_s(a, b), c_in)
sum
c_o
(h_s(a, b), c_in)
OR
c_out
32
FA0
a1
b1
sum0
FA1
a2
b2
sum1
FA2
sum2
a31
b31
ELEC2200-002 Lecture 2
FA31
sum31
33
ELEC2200-002 Lecture 2
34
16-bit
ripple
carry
adder
16-bit
ripple
carry
adder
16-bit
ripple
carry
adder
sum0-sum15
0
Multiplexer
a0-a15
sum16-sum31
ELEC2200-002 Lecture 2
35
Fast Adders
In general, any output of a 32-bit adder can
be evaluated as a logic expression in terms
of all 65 inputs.
Number of levels of logic can be reduced to
log2N for N-bit adder. Ripple-carry has N
levels.
More gates are needed, about log 2N times
that of ripple-carry design.
Fastest design is known as carry
lookahead adder.
Fall 2012, Aug 20 . . .
ELEC2200-002 Lecture 2
36
Time complexity
(delay)
Space complexity
(size)
Ripple-carry
O(N)
O(N)
Carry-lookahead
O(log2N)
O(N log2N)
Carry-skip
O(N)
O(N)
Carry-select
O(N)
O(N)
ELEC2200-002 Lecture 2
37
= 8ten
multiplicand
1 0 0 1 two
____________
= 9ten
multiplier
1000
0000
0000
1000
____________
1 0 0 1 0 0 0two
partial products
= 72ten
ELEC2200-002 Lecture 2
38
Multiplication Flowchart
Start
Add multiplicand to
product and place result
in product register
LSB
of multiplier
?
n = 32
n=?
ELEC2200-002 Lecture 2
n < 32
n=n+1
39
Serial Multiplication
shift left
shift right
shift
64
add
64-bit ALU
LSB
=1
64
64-bit product register
32-bit multiplier
write
ELEC2200-002 Lecture 2
Test LSB
32 times
LSB = 0
Multiplicand
32
32
add
Test LSB
32 times
32-bit ALU
LSB
1
32
64-bit product register
e
rit
shift right
ELEC2200-002 Lecture 2
41
Step
Multiplicand
Product
Initial values
0010
0000 0011
0010
0010 0011
0010
0001 0001
0010
0011 0001
0010
0001 1000
0010
0001 1000
0010
0000 1100
0010
0000 1100
0010
0000 0110
2
3
4
ELEC2200-002 Lecture 2
42
ELEC2200-002 Lecture 2
43
Step
Multiplicand
Product
Initial values
11010
00000 0011
11010
11010 0011
11010
11101 0001
11010
10111 0001
11010
11011 1000
11010
11011 1000
11010
11101 1100
11010
11101 1100
11010
11110 1110
2
3
4
ELEC2200-002 Lecture 2
44
Step
Multiplicand
Product
Initial values
11010
00000 1011
11010
11010 1011
11010
11101 0101
11010
10111 0101
11010
11011 1010
11010
11011 1010
11010
11101 1101
00110
00011 1101
11010
00001 1110
2
3
4
ELEC2200-002 Lecture 2
45
multiplicand
multiplier
four
partial
products
to be
summed
ELEC2200-002 Lecture 2
46
multiplicand
multiplier
four
partial
products
to be
summed
ELEC2200-002 Lecture 2
47
x0
yi
xk
sum bit
from (k-1)th
sum
carry bits
from (k-1)th
sum
Full
adder
p0i = x0yi
0 partial product
th
carry bits
to (k+1)th
sum
ELEC2200-002 Lecture 2
ith bit of
kth partial
product
Slide 24
sum bit
to (k+1)th
sum
48
y3
Array Multiplier
ppk
yj
xi
y2
y1
y0
x0
0
x1
ci
FA
x2
co
ppk+1
0
x3
Critical path
0
FA
p7
Fall 2012, Aug 20 . . .
FA
p6
FA
p5
FA
p4
ELEC2200-002 Lecture 2
0
p3
p2
p1
p0
49
ELEC2200-002 Lecture 2
50
Quotient
00001101
Divisor / Dividend 1 0 1 1 / 1 0 0 1 0 0 1 1
1011
Partial remainder
001110
1011
Remainder
001111
1011
100
ELEC2200-002 Lecture 2
51
Iteration 4 Iteration 3
6
= 1, remainder 2
4
Iteration 1
Dividend: 6 = 0110
Divisor: 4 = 0100
4 = 1100
0001
0000110
1100
1100
Iteration 2
0100
0000110
1100
1101
0100
000110
1100
1111
restore remainder
0100
00110
1 0 20
ELEC2200-002 1
Lecture
restore remainder
52
$R and $M have
one extra sign bit
beyond 32 bits.
$R $R $M
$Q0 = 1
No
Yes
$R < 0?
count = count 1
No
count = 0?
$R (33 b) | $Q (32 b)
Yes
ELEC2200-002 Lecture 2
$Q0=0
$R $R + $M
Restore $R
(remainder)
Done
$Q = Quotient
$R = Remainder
53
count
4
$R, $Q
$M = Divisor
Initialize
00000|0110
00100
00000|1100
00100
Add $M (11100) to $R
11100|1100
00100
00000|1100
00100
00001|1000
00100
Add $M (11100) to $R
11101|1000
00100
00001|1000
00100
00011|0000
00100
Add $M (11100) to $R
11111|0000
00100
00011|0000
00100
00110|0000
00100
Add $M (11100) to $R
00010|0000
00100
Set LSB of $Q = 1
00010|0001
Remainder | Quotient
00100
ELEC2200-002 Lecture 2
54
Division
33-bit $M (Divisor)
33
33 Step 2: Subtract $R $R $M
33-bit ALU
Initialize
Step 1: 1- bit left shift $R and $Q 32 times
33
$R0
33-bit $R (Remainder)
32-bit $Q (Dividend)
Step 3: If sign-bit ($R) = 0, set Q0 = 1
If sign-bit ($R) = 1, set Q0 = 0 and restore $R
V. C. Hamacher, Z. G. Vranesic and S. G. Zaky, Computer Organization, Fourth Edition,
New York: McGraw-Hill, 1996.
Fall 2012, Aug 20 . . .
ELEC2200-002 Lecture 2
55
Initialize
$R
= 00000
$Q =
1 0 0 0 $M =
= 00001
= 11101
= 11110
$Q =
0000
$Q =
0000
$R
= 00010
$M = 1 1 1 0 1
$R = 1 1 1 1 1
$Q =
0 0 0 0 $M =
Step 3, Set Q0
Restore + $M = 0 0 0 1 1
$R = 0 0 0 1 0
$Q =
0000
Iteration 2
Step 1, L-shift $R
Step 2, Add
$M
$R
Step 3, Set Q0
Restore + $M
$R
Step 1, L-shift
Step 2, Add
00011
= 00011
= 00001
ELEC2200-002 Lecture 2
00011
56
Iteration 4
Iteration 3
$R
= 00010
$Q =
0 0 0 0 $M =
00011
= 00100
= 11101
= 00001
$Q =
0 0 0 0 $M =
00011
$Q =
0001
$R,Q = 0 0 0 1 0
$M = 1 1 1 0 1
$R = 1 1 1 1 1
$Q =
0 0 1 0 $M =
Step 3, Set Q0
Restore + $M = 0 0 0 1 1
$R
= 00010
$Q =
0 0 1 0 Final quotient
Step 1, L-shift $R
Step 2, Add
$M
$R
Step 3, Set Q0
Step 1, L-shift
Step 2, Add
00011
Remainder
Note Restore $R in Steps 1, 2 and 4. This method is known as
the RESTORING DIVISION. An improved method, NON-RESTORING
DIVISION, is possible (see Hamacher, et al.)
Fall 2012, Aug 20 . . .
ELEC2200-002 Lecture 2
57
Non-Restoring Division
Avoid unnecessary addition (restoration).
How it works?
Initially $R contains dividend 2 n for n-bit numbers. Example (n = 8):
Dividend
00101101
$R, $Q
00000000 00101101
ELEC2200-002 Lecture 2
58
Non-restoring division
Initialize and start iterations same as in restoring
division by subtracting divisor
In any iteration after left shift and subtraction/addition
If $R is positive, subtract divisor (y) in next iteration
If $R is negative, add divisor (y) in next iteration
ELEC2200-002 Lecture 2
59
Iteration 2
Iteration 1
Initialize
$R
= 00000
$Q =
1 0 0 0 $M =
Step 1, L-shift $R = 0 0 0 0 1
Step 2, Add
$M = 1 1 1 0 1
$R = 1 1 1 1 0
Step 3, Set Q0
$Q =
0000
$Q =
0000
Step 1, L-shift
Step 2, Add
$Q =
0 0 0 0 $M =
$Q =
0000
$R
= 11100
+ $M = 0 0 0 1 1
$R = 1 1 1 1 1
00011
00011
Step 3, Set Q0
Add + $M in next iteration
ELEC2200-002 Lecture 2
60
Iteration 4
Iteration 3
$R
= 11111
$Q =
0 0 0 0 $M =
00011
= 11110
= 00011
= 00001
$Q =
0 0 0 0 $M =
00011
$Q =
0001
$R,Q = 0 0 0 1 0
$M = 1 1 1 0 1
$R = 1 1 1 1 1
$Q =
0 0 1 0 $M =
$Q =
0 0 1 0 Final quotient = 2
Step 1, L-shift $R
Step 2, Add
+ $M
$R
Step 3, Set Q0
Step 1, L-shift
Step 2, Add
Step 3, Set Q0
Restore + $M = 0 0 0 1 1
$R
= 00010
00011
Remainder = 2
ELEC2200-002 Lecture 2
61
Signed Division
Remember the signs and divide
magnitudes.
Negate the quotient if the signs of divisor
and dividend disagree.
There is no other direct division method
for signed division.
ELEC2200-002 Lecture 2
62
Symbol Representation
Early versions (60s and 70s)
Six-bit binary code (Control Data Corp., CDC)
EBCDIC extended binary coded decimal
interchange code (IBM)
Presently used
ASCII American standard code for information
interchange 7 bit code specified by American
National Standards Institute (ANSI), see Table 1.11
on page 63; an eighth MSB is often used as parity
bit to construct a byte-code.
Unicode 16 bit code and an extended 32 bit
version
Fall 2012, Aug 20 . . .
ELEC2200-002 Lecture 2
63
ASCII
Each byte pattern represents a character (symbol)
Convenient to write in hexadecimal, e.g., with even parity,
00000000 0ten 00hex
null
01000001 65ten 41hex
A
11100001 225ten E1hex
a
Table 1.11 on page 63 gives the 7-bit ASCII code.
C program string terminating with a null byte (odd
parity):
01000101
01000011
01000101
10000000
69ten or 45hex 67ten or 43hex 69ten or 45hex 128ten or 80hex
E
C
E
(null)
ELEC2200-002 Lecture 2
64
01000001
Parity bits
11000001
ELEC2200-002 Lecture 2
65
Richard W. Hamming
Error-correcting codes
(ECC).
Also known for
Hamming distance (HD)
= Number of bits two
binary vectors differ in
Example:
HD(1101, 1010) = 3
Hamming Medal, 1988
1915 -1998
Fall 2012, Aug 20 . . .
ELEC2200-002 Lecture 2
66
1110
E
HD = 1
HD = 1
1010
A
1-bit error in A
HD = 1
HD = 1
1000
8
1011
B
ELEC2200-002 Lecture 2
N=4
Code
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
Symbol
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F 67
0010101
HD = 4
HD = 4
HD = 3
HD = 3
1-bit error in A
shortest distance
decoding eliminates
error
0010010
HD = 1
1010010
0011110
HD = 2
HD = 4
1000111
HD = 3
HD = 3
8
1011001
B
Fall 2012, Aug 20 . . .
ELEC2200-002 Lecture 2
68
Original
code
Odd-parity
code
ECC, HD 3
0000
10000
0000000
0001
00001
0001011
0010
00010
0010101
0011
10011
0011110
0100
00100
0100110
0101
10101
0101101
0110
10110
0110011
0111
00111
0111000
1000
01000
1000111
1001
11001
1001100
1010
11010
1010010
1011
01011
1011001
1100
11100
1100001
1101
01101
1101010
1110
01110
1110100
1111
11111
1111111
ELEC2200-002 Lecture 2
69
ELEC2200-002 Lecture 2
70
ELEC2200-002 Lecture 2
71
Scientific Notation
Decimal numbers
0.513105, 5.13104 and 51.3103 are written in
scientific notation.
5.13104 is the normalized scientific notation.
Binary numbers
Base 2
Binary point multiplication by 2 moves the point
to the right.
Normalized scientific notation, e.g., 1.0two2 1
ELEC2200-002 Lecture 2
72
ELEC2200-002 Lecture 2
73
(-1)S (1.b1b2b3b4) 2E
Decimal
Example:
ELEC2200-002 Lecture 2
74
b. 1933, Canada
Professor of Computer Science, UC-Berkeley
Fall 2012, Aug 20 . . .
ELEC2200-002 Lecture 2
75
Positive underflow
Negative underflow
Negative zero
Negative
Overflow
Positive zero
Expressible
positive
numbers
Expressible
negative
numbers
- (2 2-23)2128
-2-127
2-127
+
Positive
Overflow
(2 2-23)2128
ELEC2200-002 Lecture 2
76
ELEC2200-002 Lecture 2
77
Examples
Biased exponent (0-255), bias 127 (01111111) to be subtracted
1.1010001 210100 = 0 10010011 10100010000000000000000 = 1.6328125 220
-1.1010001 210100 = 1 10010011 10100010000000000000000 = -1.6328125 220
1.0
0.5
0.125
0.0078125
1.6328125
Fall 2012, Aug 20 . . .
ELEC2200-002 Lecture 2
78
Sign bit S
F
bits 0-22
1 10000001 01000000000000000000000
ELEC2200-002 Lecture 2
79
normalized E
bits 23-30
F
bits 0-22
1 1011001 01001100000000010001101
Positive integer 127 = E
Positive underflow
Negative underflow
Negative
Overflow
- (2 2
Expressible
negative
numbers
-23
)2
127
+0
Expressible
positive
numbers
-2-126
2-126
ELEC2200-002 Lecture 2
Positive
Overflow
(2 2-23)2127
80
Fraction
+ 1.0 2127
Smallest positive number in single-precision
IEEE 754 standard.
Interpreted as positive zero.
True exponent less than 126 is positive
underflow; can be regarded as zero.
Fall 2012, Aug 20 . . .
ELEC2200-002 Lecture 2
81
Fraction
1.0 2127
Smallest negative number in single-precision
IEEE 754 standard.
Interpreted as negative zero.
True exponent less than 126 is negative
underflow; may be regarded as 0.
Fall 2012, Aug 20 . . .
ELEC2200-002 Lecture 2
82
Fraction
+ 1.0 2128
Largest positive number in single-precision IEEE
754 standard.
Interpreted as +
If true exponent = 128 and fraction 0, then the
number is greater than . It is called not a
number or NaN and may be interpreted as .
Fall 2012, Aug 20 . . .
ELEC2200-002 Lecture 2
83
Fraction
1.0 2128
Smallest negative number in single-precision
IEEE 754 standard.
Interpreted as -
If true exponent = 128 and fraction 0, then the
number is less than - . It is called not a number
or NaN and may be interpreted as - .
Fall 2012, Aug 20 . . .
ELEC2200-002 Lecture 2
84
4. Rounding
Fall 2012, Aug 20 . . .
ELEC2200-002 Lecture 2
85
Step 1:
Step 2:
01000
+11001
00001
ELEC2200-002 Lecture 2
86
Example (Continued)
Step 3: Normalize, 1.000two 2
No overflow/underflow since
127 exponent 126
4=
(1+0)/16 = 0.0625ten
ELEC2200-002 Lecture 2
87
overflow/underflow
5. Replace sign
ELEC2200-002 Lecture 2
88
FP Multiplication: Step 0
Multiply, X Y = Z
X = 0?
yes
no
Y = 0?
no
Steps 1 - 5
yes
Z=0
Return
ELEC2200-002 Lecture 2
89
FP Multiplication Illustration
Multiply 0.5ten and 0.4375ten
(answer = 0.21875ten) or
Multiply 1.000two2 1 and 1.110two2
Step 1: Add exponents
1 + (2) = 3
Step 2: Multiply significands
1.000
1.110
0000
1000
1000
1000
1110000
Fall 2012, Aug 20 . . .
Product is 1.110000
ELEC2200-002 Lecture 2
90
ELEC2200-002 Lecture 2
91
ELEC2200-002 Lecture 2
92