PPT#04
PPT#04
D IIgor Ivkovic
Dr. I k i
[email protected]
@
[with material from “Computer Organization and Design” by Patterson and Hennessy, and “Digital Design and
Computer Architecture” by Harris and Harris, both published by Morgan Kaufmann]
Objectives
2
Unsigned Binary Integers
Given an n-bit number:
Example:
0000 0000 0000 0000 0000 0000 0000 10112
= 0 + … + 1×23 + 0×22 +1×21 +1×20
= 0 + … + 8 + 0 + 2 + 1 = 1110
Using 32 bits:
Represent 0 to +4,294,967,295
H
How d
do we representt negative
ti numbers?
b ?
3
One Idea: Signed/Magnitude Binary Integers
Given an n-bit number:
5
Better Idea: Two’s Complement Integers /2
Bit 31 or the left-most bit:
The most significant bit (MSB) or the sign bit
MSB 1111 1111 1111 1111 1111 1111 1111 11002
MSB = 1 for negative numbers
MSB = 0 for non-negative numbers
Example:
Start with 11002 = –23 + 22 = –8 + 4 = –410
6
Better Idea: Two’s Complement Integers /3
Some specific numbers:
Most positive: 0111 1111 … 1111
2
2: 0000 0000 … 0010
1: 0000 0000 … 0001
0: 0000 0000 … 0000
–1: 1111 1111 … 1111 Notice the pattern for the
–2: 1111 1111 … 1110 negative numbers: counting
the binary numbers in reverse
–3:
3: 1111 1111 … 1101
–4: 1111 1111 … 1100
–5: 1111 1111 … 1011
–6:
6: 1111 1111 … 1010
–7: 1111 1111 … 1001
–8: 1111 1111 … 1000
Most negative: 1000 0000 … 0000
7
Signed Negation
How to negate a signed integer?
Easy: Compute the complement and add 1
Compute the complement means convert 1 → 0 and 0 → 1
x + x = 1111...1112 = −1
x + 1 = −x
Example: Negate +2
+2 = 0000 0000 … 00102
–2 = 1111 1111 … 11012 + 1
= 1111 1111 … 11102
Interesting: What if you have to negate 0?
8
Sign Extension
Representing a number using more bits
The first goal is to preserve the numeric value
Example: 8
8-bit
bit to 16-bit
16 bit
+2: 0000 0010 => 0000 0000 0000 0010
–2: 1111 1110 => 1111 1111 1111 1110
9
Logical Operations
Instructions for bitwise manipulation:
10
Shift Operations
Shift left logical (sll)
Shift left and fill with 0 bits
sll by i bits multiplies by 2i
Example: 11001 << 2 = 00100
Shift right
i ht logical
l i l (srl)
( l)
Shift right and fill with 0 bits
srl by i bits divides by 2i (unsigned only)
Example: 11001 >> 2 = 00110
11
AND Operation
AND Operation
Useful to mask bits in a word
Select some bits, clear others to 0
MIPS: and $t0, $t1, $t2
12
OR Operation
OR Operation
Useful to include bits in a word
Set some bits to 1, leave others unchanged
MIPS: or $t0, $t1, $t2
13
NOT Operation
NOT Operation
Useful to invert bits in a word
Change 0 to 1, and 1 to 0
MIPS has a NOR 3-operand instruction
a NOR b == NOT ( a OR b ) Register
R i t $0 always
l
reads zero
MIPS : nor $t0, $t1, $0
14
Arithmetic Circuitry
Digital building blocks:
So far, we have covered gates, multiplexers, decoders, and
registers
i t
We will use these to build arithmetic circuits, counters, memory
arrays, and logic arrays
15
Adders /1
Adders:
Add two input bits, A
and
dBB, andd output
t t the
th A B A B
result, S
Cout Cout Cin
If there is carry-over, it + +
is outputted as Cout S S
16
Adders /2
Adders:
Half adder has no
carry-over input
i t ffrom A B A B
the previous adder
Cout Cout Cin
+ +
Full adder represents
a chained adder and S S
17
Adders /3
Multibit Adders (CPAs)
When composing multibit adders, different strategies for
propagating
ti carry-over bits
bit apply
l
Symbol:
18
Ripple-Carry Adder
Ripple-Carry Adder
Simply chain 1-bit adders together
Carry ripples through entire chain
Advantage: simpler operational semantics
Disadvantage: slower performance
19
Carry-Lookahead Adder /1
Carry-Lookahead Adder
Instead of waiting for each adder to compute the carry-out bit,
di id the
divide th addition
dditi iinto
t kk-bit
bit bl
blocks
k and
d precompute
t carry-outs
t
Compute carry-out (Cout) for k-bit blocks using generate, Gi,
and propagate, Pi, signals
Operational Semantics:
Column i adder produces a carry-out by either generating a
carry-outt or propagating
ti a carry-in
i to
t the
th carry-outt
Generate (Gi) and propagate (Pi) signals for each column:
Column i will generate a carry-out if Ai AND Bi are both 1
Gi = Ai Bi
Column i will propagate a carry-in to the carry-out if Ai OR Bi is 1
Pi = Ai + Bi
The carry-out
carry out of column i (Ci) is:
Ci = Ai Bi + (Ai + Bi)Ci-1 = Gi + Pi Ci-1
20
Carry-Lookahead Adder /2
G i = Ai Bi
Pi = Ai + Bi
Ci = Ai Bi + (Ai + Bi)Ci-1
= Gi + Pi Ci-1
21
Carry-Lookahead Adder /3
Operational Semantics Continued:
Step 1. Compute Gi and Pi for all columns
Step 2. Compute G and P for k-bit blocks
Step 3. Cin propagates through each k-bit P/G block
E
Example:
l 4-bit
4 bit blocks
bl k (G3:0 and
d P3:0)
G3:0 = G3 + P3 (G2 + P2 (G1 + P1G0 )
P3:0 = P3P2P1P0
22
Carry-Lookahead Adder /4
Carry-Lookahead Adder Delay
For N-bit CLA with k-bit blocks:
tCLA = tpg + tpg_block + (N/k – 1) x tAND_OR + k x tFA
23
Prefix Adder /1
Prefix Adder
Continues on the idea of carry-lookahead adder
Computes carry-in
carry in (Ci-1)
(Ci 1) for each column then computes the sum
Si = (Ai ⊕ Bi) ⊕ Ci
Computes G and P for 1-, 2-, 4-, 8-, etc bit blocks until all Gi
((carry-in)
y ) are known ((computed
p in log
g2N stages)
g )
Operational Semantics Overview:
Carry-in either generated in a column or propagated from a
previous column
Column -1 holds Cin, so G-1 = Cin and P-1 = 0
Carry-in to column i equals carry-out of column i-1: Ci-1 = Gi-1:-1
Gi-1:-1 is
i th
the generate
t signal
i l spanning
i columns
l ii-1
1 tto -1
1
Sum equation: Si = (Ai ⊕ Bi) ⊕ Gi-1:-1
Goal: Quickly compute G0:-1, G1:-1, G2:-1, G3:-1, G4:-1, G5:-1, …
Th
These are called
ll d prefixes
fi
24
Prefix tree
composed of
these nodes
25
Prefix Adder /3
Operational Semantics Continued:
Generate and propagate signals for a block spanning bits i:j:
Gi:j = Gi:k + Pi:k Gk-1:j
Pi:j = Pi:kPk-1:j
G
Generate
t Stage:
St bl
block
k i:j
i j will
ill generate
t a carry if
Upper part (i:k) generates a carry, or
Upper part propagates a carry generated in lower part (k-1:j)
(k 1:j)
26
Prefix Adder /4
Prefix Adder Delay
For N-bit prefix adder (PA):
tPA = tpg + log2N x (tpg_prefix) + tXOR
The delay
y grows
g logarithmically
g y instead of linearly
y
An N-bit PA is generally faster than a CLA for N ≥ 32
27
Adder Delay Comparisons
Let us compare the delay of 32-bit ripple-carry,
carry-lookahead, and prefix adders
CLA has 4-bit blocks
2-input gate delay = 100 ps; full adder delay = 300 ps
tripple
i l = N x tFA = 32(300 ps) = 9.6 ns
tCLA = tpg + tpg_block + (N/k – 1) x tAND_OR + k x tFA
= [100 + 600 + (7)200 + 4(300)] ps = 3.3 ns
tPA = tpg + log2N x (tpg_prefix) + tXOR
= [100 + log232(200) + 100] ps = 1.2 ns
28
Subtractor
Subtraction:
Subtract A from B by first changing the sign of B using an
i
inverter
t (i(i.e., complement
l t B’
B’s bit
bits and
d th
then add
dd 1)
Then add A and the inverted B
Cin = 1
29
Equality Comparator
Comparator:
Determines if two N-bit binary numbers, A and B, are equal, or
if one number
b is i greater
t than
th or less
l than
th the
th other
th numberb
Equality Comparator:
Produces a single output that indicates if A is equal to B
30
Magnitude Comparator
Magnitude Comparator:
Produces one or more outputs
i di ti th
indicating the relative
l ti values
l off A
and B
First compute A – B and then look
at the MSB
S off the result
If the MSB equals 1 (i.e., the result
is negative), A is less than B;
otherwise, A is greater than or
equal to B
31
Introducing Arithmetic Logic Unit (ALU) /1
Arithmetic/Logical Unit (ALU):
Combines mathematical and logical operations
A typical ALU performs AND, OR, addition, subtraction, and
magnitude comparison operations
The ALU is at the core of most computer
p systems
y
Certain ALUs produce extra outputs, called flags, that indicate
information about the ALU output
For example,
example overflow flag indicates that the result of the adder
overflowed; zero flag indicates that the result is zero
The symbol
y for ALU:
32
Introducing Arithmetic Logic Unit (ALU) /2
F2:0 Function
000 A&B
001 A|B
010 A+B
011 not used
100 A & ~B
101 A | ~B
110 A-B
111 SLT
33
Introducing Arithmetic Logic Unit (ALU) /3
0
010 A+B
F2 bit
011 not used
100 A & ~B
101 A | ~B
Extend
110 A-B
Zero
F1:0 bits
111 SLT
0
3
34
Introducing Arithmetic Logic Unit (ALU) /4
Configure 32-bit ALU for A B
N N
SLT operation:
A = 25 and B = 32
A < B, so Y should be 32-bit N
1
representation of 1
0
F2
(0x00000001) N
F2 bit
F2:0 = 111
F2 = 1 ((adder acts as
subtracter), so Cout +
25 - 32 = -7 [N-1] S
-7 has 1 in the most significant
Exttend
bit (S31 = 1)
Z
Zero
F1:0 = 11 multiplexer selects Y N N N N F1:0 bits
= S31 (zero extender) =
0
3
2
0x00000001 2 F1:0
N
Y
35
Shifters and Rotators /1
Shifters and Rotators:
Move bits and multiply or divide by powers of 2.
Shifter: Circuit that shifts a binary number left or right by a
specified number of bit positions
Rotator: Rotates a binary y number in a circle pattern,
p , so that
empty spots are filled with bits shifted from the other end
Logical Shifter: Shifts a binary number to the left (sll) or right
(srl), and fills the empty bits with 0s
Arithmetic Shifter: Performs the same as the logical shifter on
left (sla and sll are the same); on right, it fill the bits with a
copy of the MSB (sra is different than srl)
36
Shifters and Rotators /2
Shifters and Rotators Examples:
Logical shifter:
11001 >> 2 = 00110
11001 << 2 = 00100
Arithmetic shifter:
11001 >>> 2 = 11110
11001 <<< 2 = 00100
Rotator:
11001 ROR 2 = 01110
11001 ROL 2 = 00111
37
Shifters and Rotators /3
A3 A2 A1 A0 shamt1:0
shamt1:0: 2
Represents the shift magnitude
00
S1:0
01
shamt1:0 as the
If shamt1:0 = 00,, Y = A 00 selector bits
S1:0
01
10
Y2
11
00
S1:0
01
10
Y1
11
00
S1:0
01
10
Y0
Ground 11
symbol
38
Shifters and Rotators /4
Shifters and Rotators Applied:
A << N = A × 2N
00001 << 2 = 00100 (1 × 22 = 4)
11101 << 2 = 10100 (-3 × 22 = -12)
A >>> N = A / 2N
01000 >>> 2 = 00010 (8 / 22 = 2)
10000 >>> 2 = 11100 (-16 / 22 = -4)
Trace these through the matching shifters on the next slide
39
Multipliers /1
Multipliers:
N-by-N multipliers multiply two N-bit numbers, and produce
2N bit results
2N-bit lt
The partial products in binary multiplication are either the
multiplicand or all 0s
Multiplication of 1-bit binary numbers is equivalent to the AND
operation, so AND gates are used to form the partial products
A B
230 multiplicand 0101 Multiplying a single digit 4 4
x 42 multiplier x 0111 of the multiplier with
460
+ 920
partial
ti l
products
0101
0101
multiplicand in stages
x
9660 0101
+ 0000 Shifted partial products
8
are then summed to
result
lt 0100011 form the result P
230 x 42 = 9660 5 x 7 = 35 41
A3 A2 A1 A0
x B3 B2 B1 B0
A3B0 A2B0 A1B0 A0B0
A3B1 A2B1 A1B1 A0B1
A3B2 A2B2 A1B2 A0B2
+ A3B3 A2B3 A1B3 A0B3 Trace execution of
0101 x 0101
P7 P6 P5 P4 P3 P2 P1 P0
A3 A2 A1 A0
B0
B1
0
Adders add components of
the partial products 0
B2
0 Cout
B3
Q, and a remainder, R Q3
N indicates if R – B is A2
negative, and it is
1
obtained from the Cout bit
of the left-most block Q2
g A1
R B
1
R B
Cout Cin Cout Cin
i + i Q1
D
D A0
N R'
1
N 1
Q0
R'
R3 R2 R1 R0 43
Food for Thought
Download and Read Assignment #2 Specifications
Read:
ead
Chapter 3 of the course textbook
Review the material discussed in the lecture notes in more detail
(Optional) Chapter 5 of the Harris and Harris textbook
44