00000000

advertisement
MULTIPLICATION AND DIVISION
ALGORITHMS
Binary Multiplication :
147
10010011
* 85
* 01010101
Multiplicand
Multiplier
10010011
00000000
10010011
00000000
10010011
00000000
10010011
00000000
12495
011000011001111 Product
Multiply Algorithm Version 1:
Multiplication Hardware 1:
Thinking about new algorithm :
Changes can be made:
 -instead of shifting multiplicand left, shift product right.
 -adder can be shortened to length of the multiplier &
multiplicand(8 bits in our example).
 -place multiplier in right half of the product register(multiplier will
disappear as product is shifted right.
 -add multiplicand to left half of product.
Multiply Algorithm Final Version :
For FinalVersion :
Step-by-step transition in the registers
during the multiplication process (Final)
Multiplication Hardware(Final) :
Multiply in MIPS :
 Can multiply variable by any constant using MIPS sll and add
instructions:
i' = i * 10; /* assume i: $s0 */
sll $t0, $s0, 3
add $t1, $zero, $t0
sll $t0, $s0, 1
add $s0, $t1, $t0
# i * 23
# i * 21
 MIPS multiply instructions: mult, multu
 mult $t0, $t1
– puts 64-bit product in pair of new registers hi, lo; copy to
$n by mfhi, mflo
– 32-bit integer result in register lo
Multiplying Signed numbers :
Main difficulty arises when signed numbers are involved.
 Naive approach: convert both operands to positive numbers,
multiply, then calculate separately the sign of the
product and convert if necessary.
 A better approach: Booth’s Algorithm.
Booth’s idea: if during the scan of the multiplier, we observe a
sequence of 1’s, we can replace it first by subtracting the multiplicand
(instead of adding it to the product) and later, add the multiplicand,
after seeing the last 1 of the sequence.
For Example:
0110 x 0011 (6x3) This can be done by (8- 2) x3 as well, or (1000 - 0010) x 0011
(using 8 bit words)
At start, product = 00000000, looking at each bit of the multiplier 0110, from
right to left:
0: product unchanged: 00000000 ,
shift multiplicand left: 00110
1: start of a sequence:
subtract multiplicand from product: 00000000 - 00110 = 11111010 ,
shift multiplicand left: 001100
1: middle of sequence, product unchanged: 11111010,
shift multiplicand left: 0011000
0: end of sequence:
add multiplicand to product: 11111010 + 0011000 = 00010010 = 18
shift multiplicand left: 00110000
Booth’s Algorithm :
Scan the multiplier from right to left, observing, at each step, both the
current bit and the previous bit:
1. Depending on (current, previous) bits:
00 : Middle of a string of 0’s: do no arithmetic operation.
10 : Beginning of a string of 1’s: subtract multiplicand.
11 : Middle of a string of 1’s: do no arithmetic operation.
01 : End of a string of 1’s: add multiplicand.
2. Shift multiplicand to the left.
Integer Division :
0001 1001 quotient (25)
(10) 0000 1010 divided into 1111 1010
-1010
1011
-1010
10
101
1010
-1010
0 remainder
dividend (250)
Division Algorithm 1 :
Integer Division :
– ALU, Divisor, and Remainder registers: 16 bit;
– Quotient register: 8 bits;
– 8 bit divisor starts in left ½ of Divisor reg. and it is shifted
right 1 on each step
– Remainder register initialized with dividend
For Version 1:
Step-by-step transition in registors during
division process1
Division Algorithm (Final) :
Division Hardware (Final):
For Final Version:
Step-by-step transition in registers during
division process (Final)
Mult/Div Hardware :
End …
Thank you …
Download