110 likes | 259 Views
Lecture 15: Computer Arithmetic. Today’s topic Division. 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
E N D
Lecture 15: Computer Arithmetic • Today’s topic • Division
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 quotient dividend 1001 1000 1001010 -1000 10 101 1010 -1000 10 divisor remainder n-bit operands yield n-bitquotient and remainder
Division 1001tenQuotient Divisor 1000ten | 1001010tenDividend 0001001010 0001001010 0000001010 0000001010 100000000000 0001000000 00001000000000001000 Quo: 0 000001 0000010 000001001 • 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 divisor in left half Initially dividend
Divide Example • Divide 7ten (0000 0111two) by 2ten (0010two)
Divide Example • Divide 7ten (0000 0111two) by 2ten (0010two)
Optimized Divider • One cycle per partial-remainder subtraction • Looks a lot like a multiplier! • Same hardware can be used for both
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
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
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 =
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 = +1 • -7 div +2 Quo = -3 Rem = -1 • +7 div -2 Quo = -3 Rem = +1 • -7 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