Chap 3 1
Chap 3 1
Chapter 3
Arithmetic for Computers
Boolean Algebra
Boolean algebra is the basic math used
in digital circuits and computers.
A Boolean variable takes on only 2
values: {0,1} , {T,F}, {Yes, No},
etc.
There are 3 fundamental Boolean
operations:
AND, OR, NOT
AND OR NOT
A
Z A Z A Z
B B
SW.
B
What is the Boolean expression for Z?
__
Z = A.B + A.B
Maxterms
OR factor with all input variables
A B Z Minterms Maxterms
0 0 1 A+B
0 1 0
1 0 0
1 1 1
A B Z Minterms Maxterms
0 0 1 A+B
0 1 0
1 0 0
1 1 1
A B Z Minterms Maxterms
0 0 1 A+B
0 1 0
__
Z=AB+
1 0 0
AB
1 1 1
0 0 1 A+ B
0 1 0 Z = (A+ B)( A+
1 0 0 B)
1 1 1
0 0 0
0 1 0
1 0 0
1 1 1
2
3
4
5
6
7
8
9
10
11
12
13
Duals
13
A+ B =
A.B
NOR gate Alt gate rep.
AB = A+
B
Alt gate rep.
NAND gate
12b: A+AB
= A+B
13a: AB + AC + BC = AB +
AC
A
(a) Canonical sum-of-products
B
F = A.B+ A.B+
A.B
F
F = A+
A B
B F
F =XW +Y +Z(Y + XW )
= XW + Y +ZY + Z
XW
= XW (1+Z ) + Y (1+Z
)
=XW + Y
F =W XY Z + W XY Z + W
X YZ
= XYZ (W + W) + W X Y (Z +
Z)
= XYZ (1) + W X Y (1 )
= XYZ + W X Y
= X Y (Z + W )
outputs
Consists of AND, OR, NOR, NAND, and NOT
gates
Sequential circuits
Output depends on the current inputs and state of
outputs
Chapter 3 — Arithmetic for Computers — 21
Multiplexer
A multiplexer (MUX) selects data from one of N
inputs and directs it to a single output, just like a
railyard switch
4-input Mux needs 2 select lines to indicate which input to
route through
N-input Mux needs log2(N) selection lines
4-input
I0 MUX
I0
Z I1 Z
I1
I2 S 1 S0 Z
In-1
S1 S0 0 0 I0
I3
0 1 I1
Selection
contr Functional block diagram 1 0 I2
ol 1 1 I3
Truth
Table
Z3
0 1 1 0 0 0 1 0 0 0 0
A1 1 0 0 0 0 0 0 1 0 0 0
Z4 1 0 1 0 0 0 0 0 1 0 0
Z5 1 1 0 0 0 0 0 0 0 1 0
A0 Z6 1 1 1 0 0 0 0 0 0 0 1
Functional blockZdiagram
7
off button
Bit
Storage
a
Cancel
Logic gate circuit to
2. button
implement
Call this? Q Call button released – light stays on
Cancel
a
Call Blue light
Doesn’t work. Q=1 when Call=1, Bit
but doesn’t stay 1 when Call returns button Storage
Cancel
to 0some form of “memory” in the
Need
button button pressed – light turns
circuit 3. Cancel
off
Blue light
Q
Cance
l R
but ton
0 0 0 0
0 1 1 0 CARRYi+1 = Ai Bi
1 0 1 0
1 1 0 1
SUM = A B + A B = A ⊕ B
i i i i i i i
CARRYi+1 = Ai Bi
Ai
SUMi
Bi
CARRYi+
1
==AAi (B
i (BiCi +BiCi ) + Ai (BiCi +BiCi )
i ⊕Ci ) + Ai (Bi ⊕Ci )
=A i ⊕Bi ⊕Ci
Carry
Full adder
Ci+1
Ci
1-bit ALU
for
subtraction
Subtraction is
performed using 2’s
complement, i.e.
a−b=a+b
Binvert +1 CarryIn Operation Result
0 0 00 AND
0 0 01 OR
0 0 10 Addition
1 1 10 Subtraction
( a + b) = a .
Ainvert Binvert CarryIn Operation Result
0 0 0 00 AND
1
b 1 0 00 NOR
0 0 0 01 OR
0 0 0 10 Addition
0 1 1 10 Subtraction
Ai Bi
C4 C3 C2 C1
Full Ci A3 A2 A1 A0
Ci+1
Adder B3 B2 B1 B0
Si
MSB LSB
A3 B3 A2 B2 A1 B1 A0 B0
carry- C4 Stage 3
C3 Stage 2
C2 Stage 1
C1 Stage 0
C0
out FA FA FA FA 0
S3 S2 S1 S0
Note: no carry-
in
The symbol
commonly used to
represent an ALU
Length of product is
the sum of
operand lengths
Initially 0
Initially dividend
Instructions
div rs, rt / divu rs,
rt
No overflow or divide-by-0 checking
Software must perform checks if required
Use mfhi, mflo to access result
Chapter 3 — Arithmetic for Computers — 60
§3.5 Floating Point
Floating Point
Representation for non-integral numbers
Including very small and very large numbers
Like scientific notation
–2.34 × 1056 normalized
+0.002 × 10 – 4 not normalized
+987.02 × 109
In binary
±1.xxxxxxx2 × 2yyyy
Types float and double in
C
Chapter 3 — Arithmetic for Computers — 61
Floating Point Standard
Defined by IEEE Std 754-1985
Developed in response to divergence of
representations
Portability issues for scientific code
Now almost universally adopted
Two representations
Single precision (32-bit)
Double precision (64-bit)
= (–1) × 1.25 × 22
= –5.0
Step 1
Step 2
Step 3
Step 4
memory space
Compiled MIPS code:
f2c: lwc1 $f16, const5($gp)
lwc2 $f18, const9($gp)
div.s $f16, $f16, $f18
lwc1 $f18, const32($gp)
sub.s $f18, $f12, $f18
mul.s $f0, $f16, $f18
jr $ra
Chapter 3 — Arithmetic for Computers — 80
§3.9 Fallacies and Pitfalls
Right Shift and Division
Left shift by i places multiplies an integer
by 2i
Right shift divides by 2i?
Only for unsigned integers
For signed integers
Arithmetic right shift: replicate the sign bit
e.g., – 5 / 4
111110112 >> 2 = 111111102 = – 2
Rounds toward – ∞
c.f. 111110112 >>> 2 = 001111102 = +62