[slides] Microprocessor-Based Systems - 48/32-bit division algorithm

14
48/32 48/32-bit bit division division algorithm algorithm 01KTP – Microprocessor-based systems Vittorio GIOVARA Alberto GRAND

description

Thiese are the slides used for presenting the Microprocessor-Based Systems 48/32-bit division program by Vittorio Giovara and Alberto Grand.

Transcript of [slides] Microprocessor-Based Systems - 48/32-bit division algorithm

Page 1: [slides] Microprocessor-Based Systems - 48/32-bit division algorithm

48/3248/32--bitbitdivisiondivision algorithmalgorithm

01KTP – Microprocessor-based systems

divisiondivision algorithmalgorithm

Vittorio GIOVARAAlberto GRAND

Page 2: [slides] Microprocessor-Based Systems - 48/32-bit division algorithm

Formulation ofFormulation of the the problemproblem

� Our goal is to find an algorithm that is able to compute the residual of the division between a 48-bit dividend and a 32-bit between a 48-bit dividend and a 32-bit divisor using an 8086 machine.

� Input numbers are expressed according to the module and sign representation.

Page 3: [slides] Microprocessor-Based Systems - 48/32-bit division algorithm

InvarianceInvariance propertyproperty (I)(I)

� The invariance property holds true when dividing two real numbers.

� In general, given a, b ∈ R, the result of the division a / b will not change if we multiply or division a / b will not change if we multiply or divide both the dividend and the divisor by a given number k other than 0.

Example: 84 / 6 = (84 / 3) / (6 / 3)= 28 / 2= 12

Page 4: [slides] Microprocessor-Based Systems - 48/32-bit division algorithm

InvarianceInvariance propertyproperty ((II)II)

� How can this property be useful to the solution ofour problem?

� Since the 8086 processor has built-in capabilitiesfor handling 32-bit/16-bit divisions, we mayfor handling 32-bit/16-bit divisions, we maychoose k = 216 , so as to reduce the length of the operands A and B to respectively 32 and 16 bits.

� Note that the division by a power of 2 can efficiently be implemented through a right shift.

Page 5: [slides] Microprocessor-Based Systems - 48/32-bit division algorithm

InvarianceInvariance propertyproperty ((III)III)

A A / 216

= =B B / 216B B / 216

� Is this result correct?� Are the two operations still equivalent?

Page 6: [slides] Microprocessor-Based Systems - 48/32-bit division algorithm

InvarianceInvariance propertyproperty (IV)(IV)� In general, the answer is no.� This is because, by performing a right shift,

we are discarding all the least significantbits of the operands.

� Nonetheless, the result we obtain is still� Nonetheless, the result we obtain is stillclose to the true quotient.

Example:184 / 21 = 8, R = 16(184 / 10) / (21/10) ≅ 18 / 2 = 9

Page 7: [slides] Microprocessor-Based Systems - 48/32-bit division algorithm

ApproximationApproximation ofof the the quotientquotient

� The result obtained by dividing the MS 32 bits of� The result obtained by dividing the MS 32 bits ofA by the MS 16 bits of B may thus be used as aninitial approximation of the true quotient.

Page 8: [slides] Microprocessor-Based Systems - 48/32-bit division algorithm

The The algorithmalgorithm (I)(I)

� The division between A47…16 and B31…16 isinitially performed, regardless of the sign of the operands.operands.

� The approximated quotient Q is then multipliedby the divisor B, in order to obtain A* .

� A* is subsequently compared with A.

Page 9: [slides] Microprocessor-Based Systems - 48/32-bit division algorithm

The The algorithmalgorithm ((II)II)� If A > A*, then we check if their difference

is larger than B, i.e. we check whether Bfits one more time in A.◦ If this is the case, A* is incremented by B and

we go back to the initial test.we go back to the initial test.◦ Otherwise, the difference is the sought

residual.� If A > A*, then A* is decremented by B

and we go back to the initial test.� Otherwise, A = A*, so the sought residual

is 0.

Page 10: [slides] Microprocessor-Based Systems - 48/32-bit division algorithm

HandlingHandling ofof signedsigned valuesvalues (I)(I)

� The sign of the operands is stored in a register (which is then pushed onto the stack) and later retrieved to adjust the result.and later retrieved to adjust the result.

� This is achieved by means of an AND operation between the 8 MSBs of the twooperands and the bitmask 80H.

Page 11: [slides] Microprocessor-Based Systems - 48/32-bit division algorithm

HandlingHandling ofof signedsigned valuesvalues ((II)II)

� Both operands are then made positive by forcing a 0 in their MSB, by means of an AND operationbetween the 8 MSBs and the bitmask 7FH.between the 8 MSBs and the bitmask 7FH.

� The algorithm therefore treats the operands as ifthey were positive.

Page 12: [slides] Microprocessor-Based Systems - 48/32-bit division algorithm

AdjustmentAdjustment ofof the the residualresidual (I)(I)� Note that:17 / 4 = 4, R = 1 = R*

BUT

(-17) / 4 = -5, R = 3 = 4 – R*

17 / (-4) = -4, R = 1

(-17) / (-4) = 5, R = 3 = 4 – R*

Page 13: [slides] Microprocessor-Based Systems - 48/32-bit division algorithm

AdjustmentAdjustment ofof the the residualresidual ((II)II)� We can infer the rule from the previous

example:◦ If the dividend is positive, the residual is the

computed one

◦ If the dividend is negative, the residual is the ◦ If the dividend is negative, the residual is the complement to the divisor of the computed one.

� The residual is therefore adjusted if the dividend is negative.

� The original sign of the operands is finallyrestored.

Page 14: [slides] Microprocessor-Based Systems - 48/32-bit division algorithm

…that…that’s ’s allall, , folksfolks!!…that…that’s ’s allall, , folksfolks!!