Multiplication and DivisionShift multiplicand one to the left! Shift our attention of multiplier bit...

Post on 16-Mar-2020

6 views 0 download

Transcript of Multiplication and DivisionShift multiplicand one to the left! Shift our attention of multiplier bit...

Multiplication and Division

CMSC 301Prof. Szajda

Administrative

•  Read Ch. 3.1-3.4, C.11•  Program #1 due 10/24•  HW #5 assigned

w Due Monday, 10/21, at 5pm

Review

•  What is slow in a ripple carry adder?•  What makes a carry lookahead adder

faster than a ripple carry adder?

Multiplication

Multiplication in Decimal

How would this look in decimal? 0101x 0110

Multiplicand (MC)

Multiplier (MP)

Product (P)

Multiplication in Decimal

How would this look in decimal? 0101x 0110

0000

Multiplicand (MC)

Multiplier (MP)

Product (P)

Multiplication in Decimal

0101x 0110

How would this look in decimal?

00000101

Multiplicand (MC)

Multiplier (MP)

Product (P)

Multiplication in Decimal

0101x 0110

How would this look in decimal?

00000101

0101

Multiplicand (MC)

Multiplier (MP)

Product (P)

Multiplication in Decimal

0101x 0110

00000101

01010000

Multiplicand (MC)

Multiplier (MP)

Product (P)

Multiplication in Decimal

0101x 0110

0011110

00000101

01010000

Multiplicand (MC)

Multiplier (MP)

Product (P)

Observations

•  Two 4-bit operands produces an answer up to 8 bits

•  What is the only number that gets added to produce the product?

•  What happens each iteration?

Observations

•  Two 4-bit operands produces an answer up to 8 bits

•  What is the only number that gets added to produce the product? Multiplicand

•  What happens each iteration?

Observations

•  Two 4-bit operands produces an answer up to 8 bits

•  What is the only number that gets added to produce the product? Multiplicand

•  What happens each iteration?w Shift multiplicand one to the leftw Shift our attention of multiplier bit one

left

MIPS Multiplication

•  2 32-bit inputs create a 64-bit output•  MIPS places those in regs named HI

and LO•  code for $s0 = $s1 * $s2mult $s1, $s2 # implicit dests HI & LO mflo $s0 # $s0 <- LO

Multiplier Hardware

1st Multiplication Algorithm

Start

Done

No: < 32 iterations

Yes: 32 iterations

32nd iteration?

Test multiplier bit 0 MP[0] == 0?

MP[0] == 0 MP[0] != 0

P = P + MC

MC << 1

MP >> 1

1st Multiplication Algorithm

Iteration Step Multiplier Multiplicand Product 0 Initialize values 0110 0000 0101 0000 0000 1

Shift left MC Shift right MP

3 Shift left __

Shift right __

2 Shift left __

Shift right __

4 Shift left __

Shift right __

1st Multiplication Algorithm

Iteration Step Multiplier Multiplicand Product 0 Initialize values 0110 0000 0101 0000 0000 1 0 -> no op 0110

Shift left MC 0000 1010 Shift right MP 0011

3 Shift left __

Shift right __

2 Shift left __

Shift right __

4 Shift left __

Shift right __

1st Multiplication Algorithm

Iteration Step Multiplier Multiplicand Product 0 Initialize values 0110 0000 0101 0000 0000 1 0 -> no op 0110

Shift left MC 0000 1010 Shift right MP 0011

3 Shift left __

Shift right __

2 1 -> Pr = Pr + MC 0000 1010 Shift left MC 0001 0100 Shift right MP 0001

4 Shift left __

Shift right __

1st Multiplication Algorithm

Iteration Step Multiplier Multiplicand Product 0 Initialize values 0110 0000 0101 0000 0000 1 0 -> no op 0110

Shift left MC 0000 1010 Shift right MP 0011

3 1 -> Pr = Pr + MC 0001 1110 Shift left MC 0010 1000 Shift right MP 0000

2 1 -> Pr = Pr + MC 0000 1010 Shift left MC 0001 0100 Shift right MP 0001

4 Shift left __

Shift right __

1st Multiplication Algorithm

Iteration Step Multiplier Multiplicand Product 0 Initialize values 0110 0000 0101 0000 0000 1 0 -> no op 0110

Shift left MC 0000 1010 Shift right MP 0011

3 1 -> Pr = Pr + MC 0001 1110 Shift left MC 0010 1000 Shift right MP 0000

2 1 -> Pr = Pr + MC 0000 1010 Shift left MC 0001 0100 Shift right MP 0001

4 0 -> no op 0001 1110 Shift left MC 0101 0000 Shift right MP 0000

Observation of 1st Algorithm

•  Half of the MC is 0’s at any given time•  We really only care about 4 bits of the

MC •  Can we shift something else and keep

MC the same?

Observation of 1st Algorithm

•  Half of the MC is 0’s at any given time•  We really only care about 4 bits of the

MC •  Can we shift something else and keep

MC the same? Product

2nd Algorithm

•  Always add MC to the top bits of the product

•  Shift the Product to the right

2nd Multiplication Algorithm

Iteration Step Multiplier Multiplicand Product 0 Initialize values 0110 0101 0000 0000 1 0 -> no op 0110

Shift right MP 0011

3 1 -> Pr = Pr + MC

Shift right MP 0000

2 1 -> Pr = Pr + MC

Shift right MP 0001

4 0 -> no op Shift Right P Shift right MP 0000

Shift Right P

Shift Right P

Shift Right P

2nd Multiplication Algorithm

Iteration Step Multiplier Multiplicand Product 0 Initialize values 0110 0101 0000 0000 1 0 -> no op 0110

Shift right MP 0011

3 1 -> Pr = Pr + MC

Shift right MP 0000

2 1 -> Pr = Pr + MC

Shift right MP 0001

4 0 -> no op Shift Right P Shift right MP 0000

Shift Right P 0000 0000

Shift Right P

Shift Right P

2nd Multiplication Algorithm

Iteration Step Multiplier Multiplicand Product 0 Initialize values 0110 0101 0000 0000 1 0 -> no op 0110

Shift right MP 0011

3 1 -> Pr = Pr + MC

Shift right MP 0000

2 1 -> Pr = Pr + MC 0101 0000

Shift right MP 0001

4 0 -> no op Shift Right P Shift right MP 0000

Shift Right P 0000 0000

Shift Right P 0010 1000

Shift Right P

2nd Multiplication Algorithm

Iteration Step Multiplier Multiplicand Product 0 Initialize values 0110 0101 0000 0000 1 0 -> no op 0110

Shift right MP 0011

3 1 -> Pr = Pr + MC 0111 1000

Shift right MP 0000

2 1 -> Pr = Pr + MC 0101 0000

Shift right MP 0001

4 0 -> no op Shift Right P Shift right MP 0000

Shift Right P 0000 0000

Shift Right P 0010 1000

Shift Right P 0011 1100

2nd Multiplication Algorithm

Iteration Step Multiplier Multiplicand Product 0 Initialize values 0110 0101 0000 0000 1 0 -> no op 0110

Shift right MP 0011

3 1 -> Pr = Pr + MC 0111 1000

Shift right MP 0000

2 1 -> Pr = Pr + MC 0101 0000

Shift right MP 0001

4 0 -> no op 0011 1100 Shift Right P 0001 1110 Shift right MP 0000

Shift Right P 0000 0000

Shift Right P 0010 1000

Shift Right P 0011 1100

Savings

•  We have a 4-bit adder, not 8-bit adder•  Save 4 bits of storage in Multiplicand

Observations about 2nd Algorithm

•  The # of bits we care about in the Multiplier is equal to the # of 0’s in Product

Observations about 2nd Algorithm

•  The # of bits we care about in the Multiplier is equal to the # of 0’s in Product

•  SolutionStore the Multiplier in the lower

portion of the Product

3rd Multiplication Algorithm

Iteration Step Multiplicand Product / MP 0 Initialize values 0101 0000 0110 1 0 -> no op

3 1 -> Pr = Pr + MC

2 1 -> Pr = Pr + MC

4 0 -> no op Shift Right P

Shift Right P

Shift Right P

Shift Right P

3rd Multiplication Algorithm

Iteration Step Multiplicand Product / MP 0 Initialize values 0101 0000 0110 1 0 -> no op

3 1 -> Pr = Pr + MC

2 1 -> Pr = Pr + MC

4 0 -> no op Shift Right P

Shift Right P 0000 0011

Shift Right P

Shift Right P

3rd Multiplication Algorithm

Iteration Step Multiplicand Product / MP 0 Initialize values 0101 0000 0110 1 0 -> no op

3 1 -> Pr = Pr + MC

2 1 -> Pr = Pr + MC 0101 0011

4 0 -> no op Shift Right P

Shift Right P 0000 0011

Shift Right P 0010 1001

Shift Right P

3rd Multiplication Algorithm

Iteration Step Multiplicand Product / MP 0 Initialize values 0101 0000 0110 1 0 -> no op

3 1 -> Pr = Pr + MC 0111 1001

2 1 -> Pr = Pr + MC 0101 0011

4 0 -> no op Shift Right P

Shift Right P 0000 0011

Shift Right P 0010 1001

Shift Right P 0011 1100

3rd Multiplication Algorithm

Iteration Step Multiplicand Product / MP 0 Initialize values 0101 0000 0110 1 0 -> no op

3 1 -> Pr = Pr + MC 0111 1001

2 1 -> Pr = Pr + MC 0101 0011

4 0 -> no op Shift Right P 0001 1110

Shift Right P 0000 0011

Shift Right P 0010 1001

Shift Right P 0011 1100

Savings

•  Only one shift per iteration•  Save 4 bits of storage from Multiplier

•  Limitation: 4 * -1?

Division

Division in Decimal

0111

How would this look in decimal?

Dividend 0010

Quotient

Divisor

Division in Decimal

0111

How would this look in decimal?

Dividend 0010

Quotient 11

-0010

0011-0010 1 Remainder

Divisor

Divisor Hardware

Initially dividend

Initially divisor in left half

1st Division AlgorithmStart

Done

Test Remainder

33rd repetition?

Divisor >> 1

Quotient << 1 Rightmost bit = 1

Rem = Divisor + Rem Quotient << 1

Rightmost bit = 0

Rem = Rem - Divisor

Rem >= 0 Rem < 0

Yes: 33 repetitions

No: < 33 repetitions

1st Division AlgorithmIteration Step Quotient Divisor Remainder

0 Initialize values 0000 0010 0000 0000 0111 1 Rem = Rem - Div

Div>>1

3 Rem = Rem - Div

Div>>1

2 Rem = Rem - Div

Div>>1

4 Rem = Rem - Div

Div>>1 5 Rem = Rem - Div

Div>>1

1st Division AlgorithmIteration Step Quotient Divisor Remainder

0 Initialize values 0000 0010 0000 0000 0111 1 Rem = Rem - Div

Rem<0=>+Div, Q<<1, Q0=0 Div>>1

3 Rem = Rem - Div

Div>>1

2 Rem = Rem - Div

Div>>1

4 Rem = Rem - Div

Div>>1

1110 0111 0000 0111

0001 0000 0000

5 Rem = Rem - Div

Div>>1

1st Division AlgorithmIteration Step Quotient Divisor Remainder

0 Initialize values 0000 0010 0000 0000 0111 1 Rem = Rem - Div

Rem<0=>+Div, Q<<1, Q0=0 Div>>1

3 Rem = Rem - Div

Div>>1

2 Rem = Rem - Div 1111 0111 Rem<0=>+Div, Q<<1, Q0=0

Div>>1

4 Rem = Rem - Div

Div>>1

1110 0111 0000 0111

0001 0000 0000

0000 0000 1000

0000 0111

5 Rem = Rem - Div

Div>>1

1st Division AlgorithmIteration Step Quotient Divisor Remainder

0 Initialize values 0000 0010 0000 0000 0111 1 Rem = Rem - Div

Rem<0=>+Div, Q<<1, Q0=0 Div>>1

3 Rem = Rem - Div 1111 1111 Rem<0=>+Div, Q<<1, Q0=0

Div>>1

2 Rem = Rem - Div 1111 0111 Rem<0=>+Div, Q<<1, Q0=0

Div>>1

4 Rem = Rem - Div

Div>>1

1110 0111 0000 0111

0001 0000 0000

0000

0000

0000 1000

0000 0100

0000 0111

0000 0111

5 Rem = Rem - Div

Div>>1

1st Division AlgorithmIteration Step Quotient Divisor Remainder

0 Initialize values 0000 0010 0000 0000 0111 1 Rem = Rem - Div

Rem<0=>+Div, Q<<1, Q0=0 Div>>1

3 Rem = Rem - Div 1111 1111 Rem<0=>+Div, Q<<1, Q0=0

Div>>1

2 Rem = Rem - Div 1111 0111 Rem<0=>+Div, Q<<1, Q0=0

Div>>1

4 Rem = Rem - Div 0000 0011 Rem>=0=> Q<<1, Q0=1

Div>>1

1110 0111 0000 0111

0001 0000 0000

0000

0000

0001

0000 1000

0000 0100

0000 0010

0000 0111

0000 0111

5 Rem = Rem - Div

Div>>1

1st Division AlgorithmIteration Step Quotient Divisor Remainder

0 Initialize values 0000 0010 0000 0000 0111 1 Rem = Rem - Div

Rem<0=>+Div, Q<<1, Q0=0 Div>>1

3 Rem = Rem - Div 1111 1111 Rem<0=>+Div, Q<<1, Q0=0

Div>>1

2 Rem = Rem - Div 1111 0111 Rem<0=>+Div, Q<<1, Q0=0

Div>>1

4 Rem = Rem - Div 0000 0011 Rem>=0=> Q<<1, Q0=1

Div>>1

1110 0111 0000 0111

0001 0000 0000

0000

0000

0001

0000 1000

0000 0100

0000 0010

0000 0111

0000 0111

5 Rem = Rem - Div 0000 0001 Rem>=0=> Q<<1, Q0=1

Div>>1 0011

0000 0001

Observation of 1st Algorithm

•  Half of the Divisor is 0’s at any given time

•  We really only care about 4 bits of the Divisor

•  Can we shift something else and keep Divisor the same?

Observation of 1st Algorithm

•  Half of the Divisor is 0’s at any given time

•  We really only care about 4 bits of the Divisor

•  Can we shift something else and keep Divisor the same?

•  Solution: w Shift the Remainder to the left

Observation of 1st Algorithm

•  First step cannot produce 1 in Quotient bitw  If it did, too large for quotient register

•  Shift first, subtract secondw Removes one iterationw Remainder in left half of Remainder

register

More Observations (of 2nd Algorithm)

•  The # of bits we care about in the Quotient is equal to the # of 0’s in Remainder

•  SolutionStore the Quotient in the lower

portion of the Remainder

3rd Division AlgorithmStart

Done. (LH)Rem >> 1

Test Remainder

32nd repetition?

Left half Rem = (LH)Rem - Divisor

Rem << 1 Rightmost bit Rem = 1

(LH)Rem = Divisor + (LH)Rem Rem << 1

Rightmost bit Rem = 0

Rem << 1

Rem >= 0 Rem < 0

Yes: 32 repetitions

No: < 32 repetitions

3rd Division Algorithm

Iteration Step Divisor Remainder 0 Initialize values 0010 0000 0111

1 Rem << 1

Rem = Rem - Div

3 Rem = Rem - Div

2 Rem = Rem - Div

4 Rem = Rem - Div

Left half of Rem >> 1

3rd Division Algorithm

Iteration Step Divisor Remainder 0 Initialize values 0010 0000 0111

1 Rem << 1

Rem = Rem - Div Rem<0=>+Div, R<<1, R0=0

3 Rem = Rem - Div

2 Rem = Rem - Div

4 Rem = Rem - Div

Left half of Rem >> 1

0000 1110 1110 1110 0001 1100

3rd Division Algorithm

Iteration Step Divisor Remainder 0 Initialize values 0010 0000 0111

1 Rem << 1

Rem = Rem - Div Rem<0=>+Div, R<<1, R0=0

3 Rem = Rem - Div

2 Rem = Rem - Div Rem<0=>+Div, R<<1, R0=0

4 Rem = Rem - Div

Left half of Rem >> 1

0000 1110 1110 1110 0001 1100 1111 1100 0011 1000

3rd Division Algorithm

Iteration Step Divisor Remainder 0 Initialize values 0010 0000 0111

1 Rem << 1

Rem = Rem - Div Rem<0=>+Div, R<<1, R0=0

3 Rem = Rem - Div Rem>=0=> R<<1, R0=1

2 Rem = Rem - Div Rem<0=>+Div, R<<1, R0=0

4 Rem = Rem - Div

Left half of Rem >> 1

0000 1110 1110 1110 0001 1100 1111 1100 0011 1000 0001 1000 0011 0001

3rd Division Algorithm

Iteration Step Divisor Remainder 0 Initialize values 0010 0000 0111

1 Rem << 1

Rem = Rem - Div Rem<0=>+Div, R<<1, R0=0

3 Rem = Rem - Div Rem>=0=> R<<1, R0=1

2 Rem = Rem - Div Rem<0=>+Div, R<<1, R0=0

4 Rem = Rem - Div Rem>=0=> R<<1, R0=1

Left half of Rem >> 1

0000 1110 1110 1110 0001 1100 1111 1100 0011 1000 0001 1000 0011 0001 0001 0001 0010 0011 0001 0011