Computer and Structure
Computer and Structure
Computer Arithmetic
Major Goals
! To introduce two's complement numbers and their addition
and subtraction.
3
Words
31 30 29 28 ... 3 2 1 0
0 1 0 0 ... 1 0 1 0
4
Possible Representations
! Three possibilities:
Sign and Magnitude One’s Complement Two’s Complement
000 = +0 000 = +0 000 = +0
001 = +1 001 = +1 001 = +1
010 = +2 010 = +2 010 = +2
011 = +3 011 = +3 011 = +3
100 = -0 100 = -3 100 = -4
101 = -1 101 = -2 101 = -3
110 = -2 110 = -1 110 = -2
111 = -3 111 = -0 111 = -1
! Issues:
" Balance
" Number of zeros
" Ease of arithmetic operations
6
Decimal to Binary Conversion
! Steps:
1. Convert N to 32-bit binary representation (sign bit is 0).
2. Invert all 32 bits (i.e., 1 to 0 and 0 to 1).
3. Add 1 to the inverted representation to obtain the two's
complement representation of -N.
! Property used:
" The sum of a binary number (x) and its inverted
representation ( x ) is 111...111two, which represents -1.
x + x = −1 ⇒ x + x + 1 = 0 ⇒ x + 1 = − x
7
Binary to Decimal Conversion
! Method 1:
" Let b i denote the i th bit of a negative number in two's
complement representation.
" Decimal representation:
b 31 × (−2 31 ) + b 30 × 2 30 + b 29 × 2 29 + ... + b1 × 21 + b 0 × 2 0
! Method 2:
" Steps:
1. Invert all 32 bits.
2. Add 1 to the inverted pattern.
3. Compute decimal representation (let b i denote the i th
bit of the inverted number; b 31 must be 0):
− (b 30 × 2 30 + b 29 × 2 29 + ... + b1 × 21 + b 0 × 2 0 )
8
Signed and Unsigned Numbers
9
Sign Extension
! Examples:
" 2 (16 bits -> 32 bits):
0000 0000 0000 0010 -> 0000 0000 0000 0000 0000 0000 0000 0010
" -2 (16 bits -> 32 bits):
1111 1111 1111 1110 -> 1111 1111 1111 1111 1111 1111 1111 1110
10
Hexadecimal Numbers
! Hexadecimal-to-binary conversion:
" 0hex - 9hex for 0000two - 1001two
" ahex - fhex for 1010two - 1111two
11
Addition and Subtraction
! Addition:
" Digits are added bit by bit from right to left, with carries
passed to the next digit to the left.
! Subtraction:
" Subtraction uses addition.
" The appropriate operand is negated before being added to
the other operand.
! Overflow:
" The result is too large to fit into a word.
12
Examples
! Addition (7 + 6 = 13):
! Subtraction (7 - 6 = 1):
13
Detecting Overflow
Addition (X + Y) Subtraction (X - Y)
14
Overflow Conditions
X+Y 0 0 1
X+Y 1 1 0
X–Y 0 1 1
X–Y 1 0 0
15
Effects of Overflow
16
Logical Operations
! One class of such operations is called shifts. They move all the
bits in a word to the left or to the right by a specified number
of bits, filling the emptied bits with 0s.
! Example:
" Original value:
0000 0000 0000 0000 0000 0000 0000 1101two
! Example:
sll $t2, $s0, 8 # $t2 gets $s0 << 8 bits
op rs rt rd shamt funct
0 0 16 10 8 0
18
Logical AND Operation
! It is a bit-by-bit operation that leaves a 1 in the result only if
both bits of the two operands are 1. A bit pattern, called mask,
can be used in conjunction with AND to force 0s where there is
a 0 in the mask.
! Example:
and $t0, $t1, $t2 # $t0 gets $t1 AND $t2
" Source operands:
Content of $t1: 0000 0000 0000 0000 0011 1100 0000 0000two
Content of $t2: 0000 0000 0000 0000 0000 1101 0000 0000two
" Destination operand:
Content of $t0: 0000 0000 0000 0000 0000 1100 0000 0000two
19
Logical OR Operation
! Example:
or $t0, $t1, $t2 # $t0 gets $t1 OR $t2
" Source operands:
Content of $t1: 0000 0000 0000 0000 0011 1100 0000 0000two
Content of $t2: 0000 0000 0000 0000 0000 1101 0000 0000two
" Destination operand:
Content of $t0: 0000 0000 0000 0000 0011 1101 0000 0000two
20
Constructing an Arithmetic Logic Unit
Processor
Control Unit
ALU
Registers
& Cache
21
Hardware Building Blocks for ALU
1. AND gate (c = a . b) a b
! AND gate:
c= a.b
0 0 0
a
c 0 1 0
b
c = a⋅b 1 0 0
1 1 1
2. OR gate (c = a + b) a b c=a+ b
! OR gate: 0 0 0
a
c 0 1 1
c =a+b b
1 0 1
1 1 1
3. Inverter (c = a)
! Inverter:
a c=a
a c 0 1
c=a
1 0
4. Multiplexor d d c
(if d = = 0, c = a;
! Multiplexor: else c = b) 0 a
a 0 1 b
a if d = 0
c
c= b 1
b if d = 1 22
Some Basics of Logic Design
! For those who are already familiar with logic design, this is an
opportunity for you to refresh your memory.
23
Digital Logic Circuits
! The electronics inside modern computers are digital. They
operate with only two voltage levels of interest - hence the use
of binary numbers.
25
Example
! Answer:
Inputs Outputs
A B C D E F
0 0 0 0 0 0
0 0 1 1 0 0
0 1 0 1 0 0
0 1 1 1 1 0
1 0 0 1 0 0
1 0 1 1 1 0
1 1 0 1 1 0
1 1 1 1 0 1
26
Boolean Algebra
! Any logic function can be implemented using only AND, OR, and
NOT operations. AND, OR, and NOT are said to form a
complete set.
27
Logic Equations
! Examples:
D = (A + B ) ⋅ C
E = ( A ⋅ C) + ( B ⋅ C )
28
Several Laws in Boolean Algebra
! Identity laws:
A+0=A A ⋅1 = A
! Zero and one laws:
A +1 =1 A⋅0 = 0
! Inverse laws:
A + A =1 A⋅A =0
! Commutative laws:
A +B =B+ A A ⋅B = B ⋅ A
! Associative laws:
A + (B + C) = ( A + B) + C A ⋅ (B ⋅ C) = ( A ⋅ B) ⋅ C
! Distributive laws:
A ⋅ (B + C) = ( A ⋅ B) + ( A ⋅ C) A + (B ⋅ C) = ( A + B) ⋅ ( A + C)
29
Example
! Answer:
D = A +B +C
E = (( A ⋅ B) + ( A ⋅ C) + (B ⋅ C)) ⋅ ( A ⋅ B ⋅ C)
F = A ⋅B ⋅C
30
Gates
! Standard representation:
AND OR NOT
31
Combinational Logic
! Other than logic gates that are the most basic building blocks,
there also exist some higher-level basic building blocks that
are also commonly used:
" Decoders/encoders
" Multiplexors
" Two-level logic and PLAs
32
Decoders
Out0
Out1
Out2
3 Out3
Decoder
Out4
Out5
Out6
Out7
a. A 3-bit decoder
33
Multiplexors
A 0
M A
u C
B 1
x
C C = ( A ⋅ S ) + (B ⋅ S)
B
S S
34
Two-Level Logic
! Sum-of-products representation:
" E.g., E = ( A ⋅ B ⋅ C ) + ( A ⋅ C ⋅ B ) + (B ⋅ C ⋅ A)
" More commonly used than product-of-sums representation.
! Product-of-sums representation:
" E.g., E = ( A + B + C ) ⋅ ( A + C + B ) ⋅ (B + C + A )
35
Example
! Answer:
D = ( A ⋅ B ⋅ C ) + ( A ⋅ B ⋅ C ) + ( A ⋅ B ⋅ C ) + ( A ⋅ B ⋅ C)
" Only those table entries for which the output is 1 generate
corresponding terms in the equation.
36
Programmable Logic Arrays
Product terms
37
Example - Problem
Inputs Outputs
A B C D E F
0 0 0 0 0 0
0 0 1 1 0 0
0 1 0 1 0 0
0 1 1 1 1 0
1 0 0 1 0 0
1 0 1 1 1 0
1 1 0 1 1 0
1 1 1 1 0 1
38
Example - Answer
! There are seven unique product terms with at least one true
value in the output section, and hence there are seven columns
in the AND plane. There are three inputs and hence the number
of rows in the AND plane is three.
Inputs
A
B
C
Outputs
D
! There are three outputs and hence the number of rows in the
OR plane is three.
39
Example - Answer (cont'd)
Inputs
A
B
AND plane
Outputs
D
OR plane E
F
40
Read-Only Memories
41
Read-Only Memories
! Typically, the contents of the memory locations in a ROM are fixed at
the time the ROM is created, and hence it is read-only storage.
42
Don't Cares
43
Example - Problem
Inputs Outputs
A B C D E F
0 0 0 0 0 0
0 0 1 1 0 1
0 1 0 0 1 1
0 1 1 1 1 X
1 0 0 1 1 X
1 0 1 1 1 X
1 1 0 1 1 X
1 1 1 1 1 X
44
Problem - Answer
! The following simplified truth table requires a PLA with only four
product terms.
Inputs Outputs
A B C D E F
0 0 0 0 0 0
0 0 1 1 0 1
0 1 0 0 1 1
X 1 1 1 1 X
1 X X 1 1 X
45
Constructing an Arithmetic Logic Unit
! Since a word in MIPS is 32 bits wide, we need a 32-bit ALU.
a
0
Result
1
b
46
1-Bit Adder
! Inputs:
" Two inputs for operands
One input for carry from right neighbor
CarryIn
"
a
! Outputs:
Sum
" One output for sum
b
" One output for carry to left neighbor
CarryOut
47
Truth Table and Logic Equations for 1-Bit Adder
! Truth table:
Inputs Outputs
Comments
a b CarryIn CarryOut SumOut
0 0 0 0 0 0 + 0 + 0 = 00two
0 0 1 0 1 0 + 0 + 1 = 01two
0 1 0 0 1 0 + 1 + 0 = 01two
0 1 1 1 0 0 + 1 + 1 = 10two
1 0 0 0 1 1 + 0 + 0 = 01two
1 0 1 1 0 1 + 0 + 1 = 10two
1 1 0 1 0 1 + 1 + 0 = 10two
1 1 1 1 1 1 + 1 + 1 = 11two
! Logic equations:
! CarryOut bit:
CarryIn
CarryOut
! SumOut bit:
??
49
1-Bit ALU (AND, OR, and Addition)
Operation
CarryIn
a
0
1
Result
2
b
CarryOut
50
32-Bit ALU
CarryIn Operation
51
Subtraction
52
1-Bit ALU (AND, OR, Addition, and Subtraction)
! Binvert is the selector input of a multiplexor that chooses
between addition and subtraction.
Binvert Operation
CarryIn
a
0
1
Result
b 0 2
CarryOut
! Key idea behind fast carry schemes without the ripple effect:
CarryIn2 = (b1 ⋅ CarryIn1) + (a1 ⋅ CarryIn1) + (a1 ⋅ b1)
CarryIn1 = (b0 ⋅ CarryIn0) + (a0 ⋅ CarryIn0) + (a0 ⋅ b0)
" Substituting the latter into the former, we have:
CarryIn2 = (a1 ⋅ a0 ⋅ b0) + (a1 ⋅ a0 ⋅ CarryIn0) + (a1 ⋅ b0 ⋅ CarryIn0)
+ (b1 ⋅ a0 ⋅ b0) + (b1 ⋅ a0 ⋅ CarryIn0) + (b1 ⋅ b0 ⋅ CarryIn0)
+ (a1 ⋅ b1)
" Other CarryIn bits can also be expressed using CarryIn0.
54