Integer Multiplication and Division
Unsigned Multiplication Signed Multiplication Faster Multiplication Unsigned Division Signed Division Multiplication and Division in MIPS
Unsigned Multiplication Paper and Pencil Exaple! Multiplicand 11002 = 12 Multiplier × 11012 = 13 1100 0000 1100 1100 Product 100111002 = 156 "#it ultiplicand $ n"#it ultiplier % &'n("#it product Accoplished via shi)ting and addition *onsues ore tie and ore chip area
+inar, ultiplication is eas, - $ ultiplicand % . $ ultiplicand % ultiplicand
Initialize Product % Multiplicand is zero extended Start
%. shift left
Multiplicand
%-
.a5 Product % Product ' Multiplicand
64 bits
0/"#it A1U
Multiplier-4
add
25 Shi)t the Multiplicand 1e)t . #it
64 bits write
Product
*ontrol
35 Shi)t the Multiplier Right . #it
64 bits shift right
Multiplier 32 bits
32nd Repetition4 7es
6o
*onsider! ..--2 $ ..-.2 9 Product % .--...--2 /"#it ultiplicand and ultiplier are used in this exaple Multiplicand is zero extended #ecause it is unsigned
Signed Multiplication :ersion . o) Signed Multiplication *onvert ultiplier and ultiplicand into positive nu#ers I) negative then o#tain the 2;s copleent and ree#er the sign
Per)or unsigned ultiplication *opute the sign o) the product I) product sign < - then o#tain the 2;s copleent o) the product
Re)ined :ersion! Use the re)ined version o) the unsigned ultiplication hard=are >hen shi)ting right9 extend the sign o) the product I) ultiplier is negative9 the last step should #e a su#tract
*ase .! Positive Multiplier Multiplicand Multiplier ×
11002 = -4 01012 = +5
11111100 111100
Product
111011002 = -20
*ase 2! 6egative Multiplier Multiplicand Multiplier ×
11002 = -4 11012 = -3
11111100 111100 00100 (2's complement of 1100)
Siilar to Unsigned Multiplier A1U produces a 33"#it result Multiplicand and I are sign"extended Sign is the sign o) the result Start
1B%Multiplier9 I%-
Multiplicand 32 bits
%.
32 bits
33"#it A1U
add, sub
First 3. iterations! I % I ' Multiplicand 1ast iteration! I % I C Multiplicand
33 bits sig
%-
1B-4
32 bits
shift right
I
1B 64 bits
*ontrol
Shi)t Right Product % &I9 1B( . #it
write LO[0]
32nd Repetition4 7es Done
6o
*onsider! ..--2 &"/( $ ..-.2 &"3(9 Product % ----..--2 Multiplicand and I are sign"extended #e)ore addition 1ast iteration! add 2;s copleent o) Multiplicand
Unsigned Division
First Division Algorith ard=are Start
!itiali"e# $e%aider = &i'ided (0)e*teded+ Lad -pper 32 bits f &i'isr .utiet = 0 shift right
.5 Shi)t the Divisor Right . #it Shi)t the uotient 1e)t . #it Di))erence % Reainder C Divisor
Divisor G-
64 bits
0/"#it A1U
sub
25 Reainder % Di))erence Set least signi)icant #it o) uotient
sig
&iffere/e
Reainder
Di))erence4
<-
write
*ontrol
64 bits shift left
uotient
32nd Repetition4 7es
6o
*onsider! ...-2 H --..2 &/"#it dividend divisor( uotient % -.--2 and Reainder % --.-2 @"#it registers )or Reainder and Divisor &@"#it A1U(
B#servations on :ersion . o) Divide :ersion . o) Division hard=are can #e optiized Instead o) shi)ting divisor right9 Shi)t the reainder register le)t as the sae net e))ect and produces the sae results Reduce ard=are! Divisor register can #e reduced to 32 #its &instead o) 0/ #its( A1U can #e reduced to 32 #its &instead o) 0/ #its( Reainder and uotient registers can #e co#ined
Re)ined Division ard=are Obser'ati#
Start
hiftig re%aider left des the sa%e as shiftig the di'isr right !itiali"e#
.5 Shi)t &Reainder9 uotient( 1e)t Di))erence % Reainder C Divisor
.utiet = &i'ided, $e%aider = 0
G-
Divisor
Di))erence4
<-
32 bits
25 Reainder % Di))erence Set least signi)icant #it o) uotient
sub
32"#it A1U
sig
&iffere/e
write
Reainder uotient 32 bits
32 bits
*ontrol
32nd Repetition4
shift left
7es
6o
Sae Exaple! ...-2 H --..2 &/"#it dividend divisor( uotient % -.--2 and Reainder % --.- 2 /"#it registers )or Reainder and Divisor &/"#it A1U(
Signed Division Siplest =a, is to ree#er the signs *onvert the dividend and divisor to positive B#tain the 2;s copleent i) the, are negative
Do the unsigned division *opute the signs o) the uotient and reainder uotient sign % Dividend sign JBR Divisor sign Reainder sign % Dividend sign
6egate the uotient and reainder i) their sign is negative B#tain the 2;s copleent to convert the to negative
1
siti'e Dividend and siti'e Divisor Exaple! '.? H '3
1
uotient % C8
Reainder % '2
egati'e Dividend and siti'e Divisor Exaple! C.? H '3
1
Reainder % '2
siti'e Dividend and egati'e Divisor Exaple! '.? H C3
1
uotient % '8
uotient % C8
Reainder % C2
egati'e Dividend and egati'e Divisor Exaple! C.? H C3
uotient % '8
Reainder % C2
Khe )ollo=ing euation ust al=a,s hold!
&i'ided = .utiet × &i'isr $e%aider
Multiplication in MIPS K=o Multipl, instructions mult
$s1,$s2 iged %ultipli/ati
multu $s1,$s2 -siged %ultipli/ati 0 1
32"#it ultiplication produces a 0/"#it Product
Separate pair o) 32"#it registers ! = high)rder 32)bit
31
LO = lw)rder 32)bit
Multipl,
Result o) ultiplication is al=a,s in I 1B
Moving data )ro IH1B to MIPS registers mfhi Rd &ove )ro I to Rd( mflo Rd &ove )ro 1B to Rd(
Divide I
1B
Division in MIPS K=o Divide instructions di
$s1,$s2
diu $s1,$s2
iged di'isi -siged di'isi 0 1
Division produces uotient and reainder
Separate pair o) 32"#it registers
31
! = 32)bit re%aider LO = 32)bit 7utiet I) divisor is - then result is upredi/table
Moving data to IH1B )ro MIPS registers mthi Rs &ove to I )ro Rs( mtlo Rs &ove to 1B )ro Rs(
Multipl, Divide I
1B
+ooth;s ultiplication algorith +ooth;s algorith involves repeatedl, adding one o) t=o predeterined values A and S to a product P 9 then per)oring a right=ard arithetic shi)t on P 5 1et % and r #e the ultiplicand and ultiplier9 respectivel,L and let x and y represent the nu#er o) #its in % and r 5 Deterine the values o) A and S9 and the initial value o) P 5 All o) these nu#ers should have a length eual to & x ' y ' .(5 A! Fill the ost signi)icant &le)tost( #its =ith the value o) %5 Fill the reaining &y ' .( #its =ith zeros5 S! Fill the ost signi)icant #its =ith the value o) &N%( in t=o;s copleent notation5 Fill the reaining &y ' .( #its =ith zeros5 P! Fill the ost signi)icant x #its =ith zeros5 Ko the right o) this9 append the value o) r 5 Fill the least signi)icant &rightost( #it =ith a zero5
Deterine the t=o least signi)icant &rightost( #its o) P 5 I) I) I) I)
the, the, the, the,
are are are are
-.9 .-9 --9 ..9
)ind the value o) P ' A5 Ignore an, over)lo=5 )ind the value o) P ' S5 Ignore an, over)lo=5 do nothing5 Use P directl, in the next step5 do nothing5 Use P directl, in the next step5
Aritheticall, shi)t the value o#tained in the 2nd step #, a single place to the right5 1et P no= eual this ne= value5 Repeat steps 2 and 3 until the, have #een done y ties5 Drop the least signi)icant &rightost( #it )ro P 5 Khis is the product o) % and r
Exaple. Find 3 $ N/9 =ith % % 3 and r % N/9 and x % / and y % /! A % --.. ---- S % ..-. ---- P % ---- ..-- Per)or the loop )our ties ! P % ---- ..-0 05 Khe last t=o #its are --5 P % ---- -..- -5 Arithetic right shi)t5
P % ---- -..0 05 Khe last t=o #its are --5 P % ---- --.. -5 Arithetic right shi)t5
P % ---- --.1 05 Khe last t=o #its are .-5 P % ..-. --.. -5 P % P ' S5 P % ...- .--. .5 Arithetic right shi)t5
P % ...- .--1 15 Khe last t=o #its are ..5 P % .... -.-- .5 Arithetic right shi)t5
Khe product is .... -.--9 =hich is N.25
Exaple2 Khe a#ove entioned techniue is inadeuate =hen t he ultiplicand is the largest negative nu#er that can #e represented &i5e5 i) the ultiplicand has @ #its then this value is N.2@(5 Bne possi#le correction to this pro#le is to add one ore #it to the le)t o) A9 S and P5 +elo=9 =e deonstrate the iproved techniue #, ultipl,ing N@ #, 2 using / #its )or the ultiplicand and the ultiplier! A % . .--- ---- S % - .--- ---- P % - ---- --.- Per)or the loop )our ties ! P % - ---- --.0 05 Khe last t=o #its are --5 P % - ---- ---. -5 Right shi)t5
P % - ---- ---1 05 Khe last t=o #its are .-5 P % - .--- ---. -5 P % P ' S5 P % - -.-- ---- .5 Right shi)t5
P % - -.-- ---0 15 Khe last t=o #its are -.5 P % . ..-- ---- .5 P % P ' A5 P % . ...- ---- -5 Right shi)t5
P % . ...- ---0 05 Khe last t=o #its are --5 P % - .... ---- -5 Right shi)t5
Khe product is ....---- &a)ter discarding the )irst and the last #it( =hich is N.05
8th9s %ultipli/ati algrith% r/edure I) x is the count o) #its o) the ultiplicand9 and y is the count o) #its o) the ultiplier ! Dra= a grid o) three lines9 each =ith suares )or x ' y ' . #its5 1a#el the lines respectivel, A &add(9 S &su#tract(9 and P &product(5 In t=o;s copleent notation9 )ill the )irst x #its o) each line =ith ! A! the ultiplicand S! the negative o) the ultiplicand P! zeroes Fill the next y #its o) each line =ith ! A! zeroes S! zeroes P! the ultiplier Fill the last #it o) each line =ith a zero5 Do #oth o) these steps y ties ! .5 I) the last t=o #its in the product are555 -- or ..! do nothing5 -.! P % P ' A5 Ignore an, over)lo=5 .-! P % P ' S5 Ignore an, over)lo=5 25 Aritheticall, shi)t the product right one position5 Drop the last #it )ro the product )or the )inal result5
:*a%ple Find 3 $ "/! A % --.. ---- S % ..-. ---- P % ---- ..-- Per)or the loop )our ties ! P % ---- ..-0 05 Khe last t=o #its are --5 P % ---- -..- -5 A right shi)t5 P % ---- -..0 05 Khe last t=o #its are --5 P % ---- --.. -5 A right shi)t5 P % ---- --.1 05 Khe last t=o #its are .-5 P % ..-. --.. -5 P % P ' S5 P % ...- .--. .5 A right shi)t5 P % ...- .--1 15 Khe last t=o #its are ..5 P % .... -.-- .5 A right shi)t5
Khe product is .... -.--9 =hich is ".25