Lecture 15: Computer Arithmetic Today’s topic –Division 1
Division Check for 0 divisor Long division approach –If divisor ≤ dividend bits 1 bit in quotient, subtract –Otherwise 0 bit in quotient, bring down next dividend bit Restoring division –Always performs subtract first –Do the subtract, and if remainder goes < 0, add divisor back Signed division –Divide using absolute values –Adjust sign of quotient and remainder as required n-bit operands yield n-bit quotient and remainder quotient dividend remainder divisor 2
3 Division 1001 ten Quotient Divisor 1000 ten | ten Dividend Quo: At every step, shift divisor right and compare it with current dividend if divisor is larger, shift 0 as the next bit of the quotient if divisor is smaller, subtract to get new dividend and shift 1 as the next bit of the quotient
Division Hardware Initially dividend Initially divisor in left half 4
5 Divide Example Divide 7 ten ( two ) by 2 ten (0010 two ) IterStepQuotDivisorRemainder 0Initial values
6 Divide Example Divide 7 ten ( two ) by 2 ten (0010 two ) IterStepQuotDivisorRemainder 0Initial values Rem = Rem – Div Rem < 0 +Div, shift 0 into Q Shift Div right Same steps as Same steps as Rem = Rem – Div Rem >= 0 shift 1 into Q Shift Div right Same steps as
Optimized Divider One cycle per partial-remainder subtraction Looks a lot like a multiplier! –Same hardware can be used for both 7
Faster Division Can’t use parallel hardware as in multiplier –Subtraction is conditional on sign of remainder Faster dividers (e.g. SRT devision) generate multiple quotient bits per step –Still require multiple steps 8
MIPS Division Use HI/LO registers for result –HI: 32-bit remainder –LO: 32-bit quotient Instructions –div rs, rt / divu rs, rt –No overflow or divide-by-0 checking Software must perform checks if required –Use mfhi, mflo to access result 9
10 Divisions involving Negatives Simplest solution: convert to positive and adjust sign later Note that multiple solutions exist for the equation: Dividend = Quotient x Divisor + Remainder +7 div +2 Quo = Rem = -7 div +2 Quo = Rem = +7 div -2 Quo = Rem = -7 div -2 Quo = Rem =
11 Divisions involving Negatives Simplest solution: convert to positive and adjust sign later Note that multiple solutions exist for the equation: Dividend = Quotient x Divisor + Remainder +7 div +2 Quo = +3 Rem = div +2 Quo = -3 Rem = div -2 Quo = -3 Rem = div -2 Quo = +3 Rem = -1 Convention: Dividend and remainder have the same sign Quotient is negative if signs (of dividend and divisor) disagree These rules fulfil the equation above