Signed Binary Addition
Signed Binary Addition
5Z032
Henk Corporaal
Eindhoven University of Technology
2011
Topics
Arithmetic
Signed and unsigned numbers
Addition and Subtraction
Logical operations
ALU: arithmetic and logic unit
Multiply
Divide
Floating Point
notation
add
multiply
32 ALU
result
32
32
TU/e Processor Design 5Z032 3
Binary numbers (1)
Bits have no inherent meaning (no semantics)
Decimal number system, e.g.:
4382 = 4x103 + 3x102 + 8x101 + 2x100
Can use arbitrary base g; value of digit c at position i:
c x gi
Binary numbers (base 2)
n-1 n-2 … 1 0 position
1100 -4 12 positive
4 0100
negative
11
-5 5
1011 0101
10
-6 9 6
1010 8 0110
-7 7
-8
1001 0111
1000
1100 -4 12 positive
4 0100
negative
11
-5 5
1011 0101
10
-6 9 6
1010 8 0110
-7 7
-8
1001 0111
1000
1100 -4 12 positive
4 0100
negative
11
-5 5
1011 0101
10
-6 9 6
1010 8 0110
-7 7
-8
1001 0111
1000
1100 -4 12 positive
4 0100
negative
11
-5 5
1011 0101
10
-6 9 6
1010 8 0110
-7 7
-8
1001 0111
1000
1100 -4 12 positive
4 0100
negative
11
-5 5
1011 0101
10
-6 9 6
1010 8 0110
-7 7
-8
1001 0111
1000
Proof:
a + a = 1111.1111b = -1 d =>
-a = a + 1
Give truth-table
operation
a result
b
CarryOut
How could we build a 1-bit ALU for add, and, and or?
How could we build a 32-bit ALU?
TU/e Processor Design 5Z032 24
Building a 32 bit ALU
CarryIn Operation
a0 CarryIn
Operation ALU0
Result0
b0
CarryIn CarryOut
a a1 CarryIn
0 ALU1
Result1
b1
CarryOut
1
Result
a2 CarryIn
Result2
ALU2
b2
2 CarryOut
b
CarryOut
a31 CarryIn
Result31
ALU31
b31
a
0
1
Result
b 0 2
CarryOut
TU/e Processor Design 5Z032 26
Tailoring the ALU to the MIPS
Need to support the set-on-less-than instruction (slt)
remember: slt rd,rs,rt is an arithmetic instruction
produces a 1 if rs < rt and 0 otherwise
use subtraction: (a-b) < 0 implies a < b
Supporting 'slt'
CarryIn
a
0
idea? b 0 2
Less 3
bits 0-30
a. CarryOut
Binvert Operation
CarryIn
a
0
Result
b 0 2
Less 3
Set
bit 31 Overflow
Overflow
detection
b.
Supporting Binvert CarryIn Operation
the ‘slt’ a0
b0
CarryIn
ALU0 Result0
operation Less
CarryOut
a1 CarryIn
b1 ALU1 Result1
0 Less
CarryOut
a2 CarryIn
b2 ALU2 Result2
0 Less
CarryOut
CarryIn
a-b = 0 a=b a0
b0
CarryIn
ALU0
Result0
Less
Notice control lines: CarryOut
32
a
zero
32
ALU result
overflow
32
b
carry-out
Can you see the ripple? How could you get rid of it?
c1 = b0c0 + a0c0 + a0b0
c2 = b1c1 + a1c1 + a1 b1 c2 = (..subst c1..)
c3 = b2c2 + a2c2 + a2 b2 c3 =
c4 = b3c3 + a3c3 + a3 b3 c4 =
Cin
Cout = Gi + Pi Cin
Cout
TU/e Processor Design 5Z032 34
Carry-lookahead adder (2)
Did we get rid of the ripple?
c1 = g0 + p0c0
c2 = g1 + p1c1 c2 = g1 + p1(g0 + p0c0)
c3 = g2 + p2c2 c3 =
c4 = g3 + p3c3 c4 =
a0 Cin 4
Result0-3
Feasible ? b0
a1
b1
a2 ALU
b2 P0
a3 G0
b3
P0 = p0.p1.p2.p3
G0= g3+(p3.g2)+(p3.p2.g1)+(p3.p2.p1.g0)
TU/e Processor Design 5Z032 35
Carry-lookahead
CarryIn
adder (3) a0
b0
a1
b1
CarryIn
ALU0
Result0--3
a2 pi
b2 P0
G0 gi
a3
b3 Carry-lookahead unit
C1
Use principle to build ci + 1
bigger adders a4
b4
CarryIn
Result4--7
a5
Can’t build a 16 bit adder b5
a6
ALU1
pi + 1
P1
b6
this way... (too big) a7
G1 gi + 1
b7
C2
Could use ripple carry of 4- ci + 2
a12 CarryIn
b12 Result12--15
a13
b13 ALU3
a14 P3 pi + 3
b14 G3 gi + 3
a15 C4
b15 ci + 4
CarryOut
TU/e Processor Design 5Z032 36
Multiplication (1)
More complicated than addition
accomplished via shifting and addition
More time and more area
Let's look at 3 versions based on gradeschool
algorithm
0010 (multiplicand)
__*_1011 (multiplier)
First implementation
Product initialized to 0 Multiplier0 = 1 1. Test Multiplier0 = 0
Multiplier0
64 bits
Yes: 32 repetitions
Done
32 bits
2. Shift the Product register right 1 bit
Multiplier
32-bit ALU Shift right
3. Shift the Multiplier register right 1 bit
32 bits
Shift right
Product Control test No: < 32 repetitions
Write 32nd repetition?
64 bits
Yes: 32 repetitions
Done
Final version
Product initialized with multiplier Product0 = 1 1. Test Product0 = 0
Product0
Multiplicand
1a. Add multiplicand to the left half of
the product and place the result in
32 bits the left half of the Product register
32-bit ALU
2. Shift the Product register right 1 bit
Yes: 32 repetitions
Done
0 1 End of 1s Add
multiplicand
0 0 Middle of 0s nothing
30
i
b a31 2 ai 2
31
i 0
Dividend
Divisor 1000/1001010\1001 Quotient
-1000
10
101
1010
-1000
10 Remainder
2.a Shift the Quotient register 2.b Restore the original value by
to the left, setting the adding the Divisor register. Also,
rightmost bit to 1 shift a 1 into the Quotient register
Divisor
Shift right Shift Divisor Register right 1 bit
64 bits
Quotient no
64-bit ALU
Shift left 33rd repetition?
32 bits
sll
srl
sra
Example:
decimal: -.75 = -3/4 = -3/22
binary : -.11 = -1.1 x 2-1
floating point: exponent = -1+bias = 126 = 01111110
IEEE single precision:
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1 0 1 1 1 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0000000000