3 Integer Arithmetic
3 Integer Arithmetic
Negative Numbers
4
2’s Complement
5
Signed Operations
6
Signed Extension
7
Binary Subtraction
8
N-bit ALU
Logic operations
Sometimes operations on individual bits needed:
and and andi can be used to turn off some bits;
or and ori turn on certain bits
Of course, AND en OR can be used for logic operations.
Note: Language C’s logical AND (&&) and OR (||) are conditional
andi and ori perform no sign extension !
operation
a result
b
A 0
C
B 1
Lets build our ALU and use a MUX to select the
outcome for the chosen operation
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 13
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
Need to support test for equality
beq $t5, $t6, label
jump to label if $t5 = $t6
use subtraction: (a-b) = 0 implies a = b
Supporting 'slt'
CarryIn
a
0
Can we figure out the Result
idea? b 0 2
1
(fig. 4.17 2nd ed.)
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.
Overflow in two’s complement
addition
° Definition: When two values of the same signs are
added:
• Result won’t fit in the number of bits provided
• Result has the opposite sign.
Overflow?
CN
CN-1
00 01 11 10 00 11
0010 0011 1110 1101 0010 1110
0011 0110 1101 1010 1100 0100
-------- -------- -------- -------- -------- --------
0101 1001 1011 0111 1110 0010
2 3 -2 -3 2 -2
3 6 -3 -6 -4 4
5 -7 -5 7 -2 2
OFL OFL
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
000 = and a1
b1
CarryIn
ALU1
Result1
001 = or 0 Less
Zero
CarryOut
010 = add
110 = subtract
a2 CarryIn
111 = slt b2 ALU2
Result2
0 Less
CarryOut
32
a
zero
32
ALU result
overflow
32
b
carry-out
0010 (multiplicand)
__*_1011 (multiplier)
Negative numbers: convert and multiply
there are better techniques, we will look at them later
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
29
Division (1)
Similar to multiplication: repeated subtract
The book discusses again three versions
We will look at one version
We have divisor( 32) , dividend(64) and quotient (32)
Three registers: The divisor and remainder 64 bits
quotient 32 bits
Initially divisor is placed in left half of divisor reg and
dividend is placed in remainder registers.
Do steps in next slide and repeat 33 times divisor +1
Division (2)
1. Substract the Divisor register from the
Remainder register and place the
result in the Remainder register
Implementation:
Repeat 33 times >= 0
Test Remainder
<0
Divisor is placed in left
half of divisor register
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 0 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
33
Modified Booth Recoding algorithm
Booth's algorithm is a multiplication algorithm that
multiplies two signed binary numbers in 2's
compliment notation.
One advantage of the Booth multiplier is, it reduce
the number of partial product. The number of steps is
reduced in Half. Can do 8 bit multiplication in 4 steps.
The main disadvantage of Booth multiplier is the
complexity of the circuit to generate a partial product
bit in the Booth encoding.
Example if multiplier is 9 and assume 5 bits then value is 01001 -> insert 0
right value becomes 0010010 then recoding groups from right are :
010 (1)
100 (-2) and 001 (1). You can insert as many 0 bits to left to the last group
the make it three bits.
If multiplier is -9 and assume 5 bits then value is 10111 -> insert 0 right
value 1101110 then recoding groups from right: 110 ( -1), 011 (2) and 110 (-1)
You can insert as many 1 to left as needed to the last group to make three 35
Modified Booth Recoding algorithm example
Assume we want to find 7 x -9. assume 5 bits are used then
7 in binary 0011 and -9 from recoding see last slide is -1, 2, -1 to find
the final product we add the following partial products:
-1 x 7 shifted 0 bits to the left,
2 x 7 shifted left 2 bits
-1 x 7 shifted left 4 bits.