s04 Math 1up
s04 Math 1up
Computer Arithmetic
data
instruction
number text chars ..............
R-format I-format ...
integer floating point
• Examples: 7 - 6 = 7 + (- 6) = 1 3 - 5 = 3 + (- 5) = -2
0 1 1 1 0 0 1 1
+ 1 0 1 0 + 1 0 1 1
• negation
• sign extension
– +3 => 0011, 00000011, 0000000000000011
– -3 => 1101, 11111101, 1111111111111101
• overflow detection
0101 5
+ 0110 6
0 1 1 1 1 0 1 0
0 1 1 1 7 1 1 0 0 -4
+ 0 0 1 1 3 + 1 0 1 1 -5
1 0 1 0 -6 0 1 1 1 7
Instruction
Decode
operation
Operand
Fetch
a
Execute 32 ALU
result
32
Result b
Store 32
Next
Instruction
CSE 141, S2'06 Jeff Brown
Designing an Arithmetic Logic Unit
ALUop
3
A
N
Zero
ALU
Result
N
Overflow
B
N
CarryOut
1 1 0 0
1 1 0 0 -4
+ 1 1 1 0 -2
1 0 1 0 -6
1 0 1 0
1 1 0 0
+ 1 0 1 1
0 1 1 1
CarryIn0
A0 1-bit Result0 X Y X XOR Y
B0 ALU
CarryOut0 0 0 0
CarryIn1
A1 0 1 1
1-bit Result1
ALU 1 0 1
B1
CarryOut1 1 1 0
CarryIn2
A2 1-bit Result2
B2 ALU
CarryIn3 CarryOut2
A3 Overflow
1-bit Result3
B3 ALU
CarryOut3
CarryOut3
CSE 141, S2'06 Jeff Brown
Set-on-less-than
• Do a subtract
• use sign bit
– route to bit 0 of result
– all other bits zero
CarryOut3
Ripple carry adders are slow. Faster addition schemes are possible that accelerate the
movement of the carry from one end to the other.
MULTIPLY
Product = ?
• m bits x n bits = m+n bit product
• Binary makes it easy:
– 0 => place 0 ( 0 x multiplicand)
– 1 => place multiplicand ( 1 x multiplicand)
• we’ll look at a couple of versions of multiplication hardware
Multiplicand Product
0110 0000 0101
0011 0010
0001 1001
0011 1100
0001 1110
CSE 141, S2'06 Jeff Brown
Observations on Multiply
Version 2
• 2 steps per bit because Multiplier & Product combined
• 32-bit adder
• MIPS registers Hi and Lo are left and right half of Product
• Gives us MIPS instruction MultU
• What about signed multiplication?
– easiest solution is to make both positive & remember whether to
complement product when done.
Quotient
Divisor 1000 1101010 Dividend
Remainder
• See how big a number can be subtracted, creating quotient bit on each step
– Binary => 1 * divisor or 0 * divisor
• Dividend = Quotient * Divisor + Remainder
Version 1
• Takes n+1 steps for n-bit Quotient & Rem. 1. Subtract the Divisor register from the
Remainder register, and place the result in the
Quotient Divisor Remainder Remainder register.
Yes: 33 repetitions
Done
CSE 141, S2'06 Jeff Brown
Divide Hardware Version 1
sign 23 -24
+6.02 x 10 1.673 x 10
Issues:
° Arithmetic (+, -, *, / )
° Representation, Normal form
° Range and Precision
° Rounding
° Exceptions (e.g., divide by zero, overflow, underflow)
° Errors
° Properties ( negation, inversion, if A = B then A - B = 0 )
exponent: mantissa:
excess 127 sign + magnitude, normalized
binary integer binary significand w/ hidden
integer bit: 1.M
(actual exponent is e = E - 127)
= 0 10000111 01000101000000000000000
.02 = .0011001101100... X 20 = 1.1001101100... X 2-3
= 0 01111100 1001101100...
• 0 0 00000000 0000000000…
• 1.5 * 2-100 0 00011011 1000000000…
• 1.75 * 2-100 0 00011011 1100000000…
• 1.5 * 2100 0 11100011 1000000000…
• 1.75*2100 0 11100011 1100000000…
exponent: mantissa:
excess 1023 sign + magnitude, normalized
binary integer binary significand w/ hidden
integer bit: 1.M
actual exponent is e = E - 1023
• Basic Algorithm
1. Align
2. Add
3. Normalize
4. Round
• Basic Algorithm
1. Add exponents
2. Multiply
3. Normalize
4. Round
5. Set Sign