Professional Documents
Culture Documents
AND CHAPTER 3
Lecture (Class A): Tue(1A-2A), Wed(7A-8A)
Lecture (Class B): Tue(2B-3B), Wed(5B-6B)
Office Hours: Tue(4A-4B), Wed(8B-9A)
This material is for educational uses only. Some contents are based on the material provided by other paper/book authors and may be copyrighted by them.
Negative Numbers
Binary Numbers
ɵ Three approaches
– Signed magnitude
– Signed-1’s complement
– Signed-2’s complement
ɵ All three approaches represent positive numbers in the same way
– Conversion of 4-digit binary code from/to decimal number
ɵ (1101)ଶ = 1 × 2ଷ + 1 × 2ଶ + 0 × 2ଵ + 1 × 2 = (13)ଵ
Divisor Dividend/Quotient Remainder
2 (13)ଵ
2 6 1
2 3 0
2 1 1
0 1
1
Signed Magnitude
ɵ Most significant bit (MSB) is the sign bit
– ɀSRVLWLYH –7 +0
– ɀQHJDWLYH –6 1111 0000 +1
–5 1110 0001 +2
ɵ Remaining bits are the number's magnitude
1101 0010
–4 +3
ɵ Arithmetic is cumbersome 1100 0011
2
Complements
ɵ Diminished radix complement of N (n-digits code)
– (r-1)’s complement for radix r defined as (rn-1) - N
– 1’s complement for binary system (radix 2)
ɵ Radix complement
– r’s complement for radix r defined as rn-N
– 2’s complement in binary system (radix 2)
ɵ Subtraction is done by adding the complement of the subtrahend
ɵ If the result is negative, takes its 2’s complement
Radix (base) is the number of unique digits including the digit zero
• Decimal system: = ݎ10 including 0,1, … , 9
• Binary system: = ݎ2 including 0,1
3
Binary 1’s Complement
ɵ For r = 2, N = (01110011)2, n = 8 (8 digits)
– (rn-1) = 256 – 1 = (255)10 or (11111111)2
– The 1’s complement of N is given by (2n - 1 - N)
11111111 (rn-1)
– 01110011 N
10001100
ɵ The 1's complement is obtained by complementing each individual bit
(bitwise NOT) because
– 2n – 1 factor consists of all 1's
– 1 – 0 = 1 and 1 – 1 = 0
4
Signed-1’s Complement
ɵ Negative number: Bitwise complement of
positive number
– ɀ10 –0 +0
– ɀ–10 –1 1111 0000 +1
ɵ Arithmetic works –2 1110 0001 +2
– Ex. 6 - 1 = 0110 + 1110 = (1)0100 + “1” 1101 0010
–3 +3
= 0101 (5) 1100 0011
– When there is end carry, add “1” to the
one’s complement result to have the real – 4 1011 0100 + 4
5
Subtraction with 1’s Complement
ɵ For n-digit unsigned numbers M and N, find M - N in minuend (M)
base 2 - subtrahend (N)
– Add the 1's complement of the subtrahend N to difference
the minuend M
ɵ M + (2n - 1 - N) = M - N + 2n - 1
– Case 1: If M N 1011 (M=(11)10)
+ 1001 (1’s comp of N=(6)10)
ɵ The sum produces an end carry rn which is
10100 (Discard end carry 24)
GLVFDUGHGIURPDERYH0î1UHPDLQV
+ 1 (Add end-around carry)
– Case 2: If M < N Answer: 0101 (=(5)10)
ɵ The sum does not produce an end carry and, 1011 (M=(11)10)
from above, is equal to 2n - 1 - (N - M), the 1's +0011 (1’s comp of N=(12)10)
complement of (N - M)
1110 (No end carry)
ɵ To obtain the result - (N – M) , take the 1's Answer: െ 0001 (= െ(1)10)
complement of the sum and place a negative
sign to its left 1’s comp.
6
Binary 2’s Complement
ɵ For = ݎ2, ܰ = (01110011)2, n = 8 (8 digit), we have
– ( ݎ ) = (256)10 or (100000000)2
ɵ The 2's complement of ܰ given by (2 െ ܰ)
100000000 ( ݎ )
– 01110011 ܰ
10001101
ɵ The 2’s complement ( ݎ െ ܰ) is equivalent to the 1's complement
plus 1, i.e., ݎ െ 1 െ ܰ + 1 = ݎ െ ܰ
– This relation can be used in designing hardware
7
Signed-2’s Complement
ɵ Negative number: Bitwise complement plus
one
–1 0
– ɀ10 –2 1111 0000 +1
– ɀ–10 –3 1110 0001 +2
1101 0010
ɵ Arithmetic works –4 +3
1100 0011
ɵ Only one zero!
– 5 1011 0100 + 4
ɵ Ex. Four-bit number in singed-2’s 1010 0101
complement system counts 16 numbers –6 1001 0110 +5
8
Subtraction with 2’s Complement
ɵ For n-digit unsigned numbers M and N, find M - N in minuend (M)
base 2 - subtrahend (N)
– Add the 2's complement of the subtrahend N to difference
the minuend M
ɵ M + (2n-N) = M - N + 2n
– Case 1: If M N 1011 (M=(11)10)
+ 1010 (2’s comp of N=(6)10)
ɵ The sum produces an end carry rn which is
10101 (Discard end carry 24)
GLVFDUGHGIURPDERYH0î1UHPDLQV
Answer: 0101 (=(5)10)
– Case 2: If M < N
ɵ The sum does not produce an end carry and, 1011 (M=(11)10)
from above, is equal to 2n - (N - M), the 2's +0100 (2’s comp of N=(12)10)
complement of (N - M)
1111 (No end carry)
ɵ To obtain the result - (N – M) , take the 2's Answer: െ 0001 (= െ(1)10)
complement of the sum and place a negative
sign to its left 2’s comp.
9
Exercise 1.
ɵ Write +6 and –6 as signed-2’s complement number
?
– +6 = 0110
– - 6 = 1010
?
10
Exercise 2.
ɵ Represent a signed number of -14 using 6 bit in three different ways
– Signed & magnitude: 101110
?
11
Exercise 3. Instruction Format of MIPS
ɵ Three instruction formats with the length of 32 bits
R type OP rs rt rd sa funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
ɵ MIPS fields
– Opcode (OP): basic operation of the instruction
– rs: the first register source operand
– rt: the second register source operand
– rd: the register destination operand getting the result of operation
– sa: shift amount
– funct: specific variant of the operation in Opcode
12
Exercise 3. Instruction Format of MIPS
ɵ MIPS instruction
– add $t0, $s1, $s2
– $s1 + $s2 ֜ $t0
R type OP rs rt rd sa funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
000000 10001 10010 01000 00000 100000
$s1 $s2 $t0
0? 17
? ?
18 8
? 0? ?
32
13
Representing Big (and Small) Numbers
(1/2)
ɵ Scientific notation
– × 1010 × 109 (normalized)
– Impossible to encode the value above in a 32-bit integer
ɵ Floating point representation
– Sign: one bit
– Fraction: a binary fraction with a non-zero leading bit
– Exponent: binary integer
֜ െ1ୱ୧୬ × × ܨ2ா
15
Example: Floating Point Number
– ?
52.21875 = 110100.00111 = .11010000111 × 2
– Normalized 23 bit fraction: .11010000111000000000000
– Excess representation for exponent: 10000101 because 127 + 6 = 133
16
IEEE Floating Point Standard 754-1985
ɵ IEEE floating point representation for binary real numbers for a 32-bit
18
Example: IEEE 754 Representation of 32-bit
Floating Point Number
20
Exercise 4. Floating Point Representation
ɵ Example
– (െ0.75)ଵ = (െ0.11)ଶ = (െ1.1)ଶ × 2ିଵ
= (െ1)ଵ × 1 + .1000 0000 0000 0000 0000 000 × 2(ିଵାଵଶ)ିଵଶ
1 0111 1110
exponent fraction
1000 0000 0000 0000 0000 000
126 (8 ܾ݅)ݏݐ (23 ܾ݅)ݏݐ
21
Floating Point Addition
ɵ Addition (and subtraction)
22
Floating Point Addition
ɵ Add
25
Floating Point Hardware
26
REVIEW FOR COMBINATIONAL AND
SEQUENTIAL LOGIC CIRCUITS
Lecture (Class A): Tue(1A-2A), Wed(7A-8A)
Lecture (Class B): Tue(2B-3B), Wed(5B-6B)
Office Hours: Tue(4A-4B), Wed(8B-9A)
Big Picture: From Compiler to Silicon
28
Big Picture: From Compiler to Silicon
Instruction set
architecture (ISA)
29
Digital Logic Gates
30
Sequential vs. Combinational
ɵ Combinational systems are memoryless
– Outputs depend only on the present inputs
Feedback 31
Functional Blocks: Addition
Combinational Logic
32
Functional Block: Binary Half-Adder
ɵ A 2-input, 1-bit width binary adder that performs the following
computations: x 0 0 1 1
+y +0 +1 +0 +1
‘cout’ ‘sum’ 00 01 01 10
ɵ A half adder adds two bits to produce a two-bit sum
ɵ The sum is expressed as a sum bit ‘sum’ and a carry bit ‘cout’
ݔ
ݕ
0 1 sum = ݔᇱ ݕ+ ݕᇱ ݔ
݉ ݉ଵ
0 1
݉ଶ ݉ଷ
1 1
ݔ
ݕ
0 1 cout = ݕݔ
݉ ݉ଵ
0
݉ଶ ݉ଷ
1 1
Truth table K-map Simplified expression 33
Implementation: Binary Half-Adder
ɵ We can draw different implementations of a binary half-adder
depending on the availability of XOR and NAND gates
x’
y cout
x x
sum y sum
x
y’ sum
x y
y cout cout
• sum = ݔᇱ ݕ+ ݕᇱ ݔ • sum = ݕ۩ݔ • sum = ( ݔ+ )ݕcoutԢ
• cout = ݕݔ • cout = ݕݔ • cout = (()ݕݔԢ)Ԣ
34
Functional Block: Binary Full-Adder
ɵ A full adder is similar to a half adder, but includes a carry-in bit ‘z’
from lower stages
ɵ Like the half-adder, it computes a sum bit ‘sum’ and a carry bit ‘cout’
ɵ For a carry-in z = 0, it is the same ɵ For a carry-in z = 1
as the half-adder
z 0 0 0 0 z 1 1 1 1
x 0 0 1 1 x 0 0 1 1
+y +0 +1 +0 +1 +y +0 +1 +0 +1
cout sum 0 0 01 01 10 cout sum 0 1 10 10 11
35
Logic Optimization: Binary Full-Adder
z ݔ
ݖݕ
00 01 11 10
0
݉ ݉ଵ
1
݉ଷ ݉ଶ
1
sum = ݔᇱ ݕᇱ ݖ+ ݔᇱ ݖݕԢ + ݕݔᇱ ݖԢ + ݖݕݔ
݉ସ ݉ହ ݉ ݉
1 1 1
ݖݕ
ݔ 00 01 11 10
0
݉ ݉ଵ ݉ଷ
1
݉ଶ
cout = ݕݔ+ ݖݔ+ ݖݕ
݉ସ ݉ହ ݉ ݉
1 1 1 1
ɵ ‘cout’ is 1 if (i) x=y=1 or (ii) the sum of x+y (i.e., )ݕ۩ݔis 1 and z=1
֜ cout = ݕݔ+ ݖ ݕ۩ݔ
This term is carry propagate
This term is carry generate
36
Implementation: Binary Full-Adder
ɵ We can draw different implementations of binary full adders
sum = ݔᇱ ݕᇱ ݖ+ ݔᇱ ݖݕԢ + ݕݔᇱ ݖԢ + ݖݕݔ cout = ݕݔ+ ݖݔ+ ݖݕ
37
Implementation: Binary Full-Adder
ɵ We can draw different implementations of binary full adders
sum = ݔᇱ ݕᇱ ݖ+ ݔᇱ ݖݕԢ + ݕݔᇱ ݖԢ + ݖݕݔ cout = ݕݔ+ ݖݔ+ ݖݕ
= ݔᇱ ݕᇱ + ݖ ݕݔ+ ݕݔᇱ + ݔᇱ ݖ ݕԢ = ݕݔ+ (ݖ)ݕ۩ݔ
= ݕ۩ݔԢ ݖ+ ݖ ݕ۩ݔԢ
= ݖ۩ ݕ۩ݔ
ݖݕ
Binary half-adder circuit ݔ 00
݉
01
݉ଵ
11
݉ଷ
10
݉ଶ
• sum = ݕ۩ݔ 0
݉ହ
1
݉
݉ସ ݉
• cout = ݕݔ 1 1 1 1
ݖݕ
ݔ 00 01 11 10
݉ ݉ଵ ݉ଷ ݉ଶ
0 1
݉ସ ݉ହ ݉ ݉
1 1 1 1
38
Implementation: Binary Full-Adder
ɵ We can draw different implementations of binary full adders
sum = ݔᇱ ݕᇱ ݖ+ ݔᇱ ݖݕԢ + ݕݔᇱ ݖԢ + ݖݕݔ cout = ݕݔ+ ݖݔ+ ݖݕ
= ݔᇱ ݕᇱ + ݖ ݕݔ+ ݕݔᇱ + ݔᇱ ݖ ݕԢ = ݕݔ+ (ݖ)ݕ۩ݔ
= ݕ۩ݔԢ ݖ+ ݖ ݕ۩ݔԢ
= ݖ۩ ݕ۩ݔ
cout
z
FA
Note that the 2nd implementation uses 2 half-adders and an OR
gate to implement the full-adder 39
Functional Block: Binary Adder
ɵ Binary adder produces the arithmetic
sum of two n-bit binary numbers ݅ܣ ܲ݅ ܲ݅۩݅ܥ
݅ܤ ܵ݅
– Full adders connected in
cascade ݅ܩ ݅ܩ+ ܲ ܥ
– The output carry from each full ܥାଵ
adder connected to the input ݅ܥ
carry of the next full adder ith stage FA
E.x.) Four-bit ripple-carry binary adder implemented from four 1-bit full adders 40
Example: 32-bit ALU
Critical path of n-bit ripple-carry adder
is n×CP (֜ carry lookahead adder)
41
Functional Block: Binary Multiplier
Multiplication
42
Functional Block: Binary Multiplier
ɵ Multiplication of more binary bits
(multiplicand) ܤ3 ܤ2 ܤ1 ܤ0
(multiplier) × ܣ2 ܣ1 ܣ0
(augend) 0 ܤ3ܣ0 ܤ2ܣ0 ܤ1ܣ0 ܤ0ܣ0
(addend) + ܤ3ܣ1 ܤ2ܣ1 ܤ1ܣ1 ܤ0ܣ1
ݔ4 ݔ3 ݔ2 ݔ1 ݔ0
+ ܤ3ܣ2 ܤ2ܣ2 ܤ1ܣ2 ܤ0ܣ2
ݕ4 ݕ3 ݕ2 ݕ1 ݕ0
ܥ6 ܥ5 ܥ4 ܥ3 ܥ2 ܥ1 ܥ0
ݔ4 ݔ3 ݔ2 ݔ1 ݔ0
ɵ For J multiplier bits and K multiplicand bits
– (JxK) AND gates and (J-1) K-bit adders to
produce (J+K) bits
43
Multiplication
ɵ Binary multiplication is just a bunch of shifts and adds
0 1 1 0 6 ଵ
0 1 0 1 5 ଵ
0 1 1 0
0 0 0 0
0 1 1 0
0 0 0 0
0 0 0 1 1 1 1 0 30 ଵ
44
Long-multiplication Approach
6 ଵ 0 1 1 0
5 ଵ 0 1 0 1
0 1 1 0
0 0 0 0
0 1 1 0
0 0 0 0
0 0 0 1 1 1 1 0 30 ଵ
45
Long-multiplication Approach
6 ଵ 0 1 1 0
5 ଵ 0 1 0 1
0 1 1 0
0 0 0 0 0 Prod=Prod+Mcand
0000+0110 ֜ Shift right Mult
0 1 1 0 1 0
0 0 0 0 1 1 0 Add in 32-bit ALU
0011+0000 ֜ Shift right
0 0 0 1 1 1 1 0 30 ଵ
Add in 32-bit ALU
0001+0110 ֜ Shift right
47
Multiplexing
ɵ Multiplexer is a digital building block for selecting from multiple inputs
and routing this input to the output, called as a data selector
– Data input: 2n lines
Control
– Control input: n-bit
S0 S1 Sn-1
– Output: 1-bit (a specific input)
ܫ
ܫଵ
Data 2n-to-1 MUX Output
ܫଶ ିଵ
48
Example: 2-to-1 Multiplexer
ɵ Illustration of 2-to-1 multiplexer
Output I0 0 S Y
Control I0
S Y Y 0 I0
1-to-2 decoder Data
I1 1 1 I1
I1
S
Logic diagram Block diagram Functional truth table
49
Example: 4-to-1 Multiplexer
ɵ Illustration of 4-to-1 multiplexer
I0
S0
Control Data S1 S0 Y
S1 Output I0 00
I1 0 0 I0
I1 01
Y Y 0 1 I1
I2 10
I3 11 1 0 I2
I2
1 1 I3
2-to-4 decoder
S1S0
I3
Logic diagram Block diagram Functional truth table
51
Functions of 32-bit ALU
52
Example: Design of 32-bit ALU (AND/OR)
ɵ A number of functions are Operation = ቊ
0, and
performed internally, but 1, or
only one result is chosen
for the output of ALU
ɵ 32-bit ALU is built out of 32
identical 1-bit ALU’s
– Adder, subtractor, etc
53
Clock
Sequential Logic
Output
A
Inputs System C
B
Settled value
Period
clock
ɵ The clock period must be long enough for all voltages to settle to a
steady state before the next state change
55
Synchronous Clocked Sequential Circuit
ɵ Clocked sequential circuits are synchronous sequential circuits using
clock pulses to control storage elements
ɵ Flip-flops are the storage element (memory) in clocked sequential
circuits
– A flip-flop is a binary storage device capable of storing 1 bit
"load"
"data" "stored bit"
57
Storage Elements
ɵ Most popular storage cells to build sequential circuits
– Latch: level sensitive storage element
– Flip-Flop: edge triggered storage element
ɵ Examples of latches
– SR latch, S’R’ latch, D latch (= gated D latch)
58
Set-Reset (SR) Latch
ɵ Cross-coupled NOR gates (with active high inputs)
– Input: ‘S’ for set and ‘R’ for reset
ɵ Input (S=1, R=0) ֜ Output (Q=1, Q’=0) in the set state
ɵ Input (S=0, R=1) ֜ Output (Q=0, Q’=1) in the reset state
R Q Q
Reset R Q
Set S Q’
S Q'
Function table
S R Q
Logic diagram
Graphic symbol 0 0 hold
0 1 0 (reset)
1 0 1 (set)
1 1 forbidden (0) 59
Behavior of SR Latch
1՜0 S R Q
0՜1՜0՜1
0 0 hold
0 1 0
1 0 1
0՜1՜0՜1 1 1 forbidden (0)
1՜0
R
S
Q
Q'
60
Glitch Sensitive SR Latch
ɵ Under normal conditions, both inputs of the latch remain at 0
ɵ Static 0 glitches can set/reset the latch
– Glitch on S input sets the latch
– Glitch on R input resets the latch
61
SR Latch with NAND Gates (S’R’ Latch)
ɵ Two cross-coupled NAND gates (with active low inputs)
– Input signals for S’R’ latch require the complement of those
values for SR latch
ɵ Input (S=0, R=1) ֜ Output (Q=1, Q’=0) in the set state
ɵ Input (S=1, R=0) ֜ Output (Q=0, Q’=1) in the reset state
ɵ Under normal conditions, both inputs of the latch remain at 1
62
Gated Latches
S’R’ latch
ɵ S’R’ latch with an additional control input (enable)
S
– Add extra NAND gates in front of S’R’ Latch. Q
ɵ When En=0, holds its current state En
– The inputs to the latch are both 1
Q'
– The S’R’ latch into its hold state R
ɵ When En=1, functions as the SR latch Logic diagram
R Q'
65
Flip-Flops (FFs)
ɵ Recall that latches are level sensitive devices and do not give precise
control with respect to when their outputs change
ɵ Restrict changes only in the instance of time when some clock signal
makes a transition from either 0 ՜ 1 (rising edge triggering) or 1 ՜ 0
(falling edge triggering)
ɵ Flip-flops are storage elements that are edge-triggered
66
What does triggering mean?
ɵ Response to positive level (a latch) – a large window of time for output to
change
67
Master-Slave DFF (Negative Edge-Triggered)
ɵ Consider a circuit constructed with two D latches (master and slave) and a inverter
ɵ The circuit samples the value of D just prior to the falling edge of the clock and
transfers it to the output Q just after the falling edge of the clock
ɵ While Clk=1 Function table of DFF
– Y will follow input D via the master latch, but Q will not follow Y (it is in hold with negative edge
state) and will hold its current value Clk Y Q
ɵ When Clk=0 (at the moment of change) 1 D hold
՝ hold D
– Y will be disconnected from D and will hold its current value
0 hold D
– Q will follow Y via the master latch ՛ D hold
ɵ A change in the output of the flip-flop can be triggered only at the transition 1 ՜ 0 68