Division
Not on your listed exam slides, but it's the mirror image of multiplication and it appears in MIPS code (% uses it). Skim this after you're solid on the other three sections.
1. Vocabulary
Division is the least frequent and quirkiest operation — it's the only one that lets you attempt something mathematically invalid: dividing by 0.
2. Long division in binary
Easier than decimal: each quotient digit is only ever 0 or 1 — the divisor either fits or it doesn't.
1101 ← Quotient (13)
┌────────
10 │ 11011 ← Dividend (27), Divisor 10 (2)
10 fits → q bit 1
--
10
10 fits → q bit 1
--
01 doesn't fit → q bit 0
11
10 fits → q bit 1
--
1 ← Remainder (1)
27 = 13 × 2 + 1 ✔3. The division algorithm (restoring division)
Hardware can't “see” whether the divisor fits. So it just subtracts anyway and checks the sign afterwards — if the result went negative the subtraction was a mistake, so it puts the value back. That's why it's called restoring division.
Initialise Remainder = Dividend, then repeat:
4. The hardware
| Register | Width | What it does |
|---|---|---|
| Divisor | 64 bits (2n) | Shifts right once per iteration (actual divisor is 32 bits, held in the top half) |
| ALU | 64-bit | Subtracts / restores |
| Quotient | 32 bits (n) | Shifts left, one new bit per iteration |
| Remainder | 64 bits (2n) | Initialised to the dividend |
Compare with multiply: the shifts run the opposite way (divisor right, quotient left) and the accumulator starts at the dividend rather than 0.
5. Worked trace — 0000 0111 ÷ 0010 (7 ÷ 2)
4-bit version ⇒ 5 iterations.
| Iter | Step | Quotient | Divisor | Remainder |
|---|---|---|---|---|
| 0 | Initial values | 0000 | 0010 0000 | 0000 0111 |
| 1 | 1: Rem = Rem − Div | 0000 | 0010 0000 | 1110 0111 |
| 2b: Rem < 0 ⇒ +Div, sll Q, Q₀ = 0 | 0000 | 0010 0000 | 0000 0111 | |
| 3: shift Divisor right | 0000 | 0001 0000 | 0000 0111 | |
| 2 | 1: Rem = Rem − Div | 0000 | 0001 0000 | 1111 0111 |
| 2b: Rem < 0 ⇒ +Div, sll Q, Q₀ = 0 | 0000 | 0001 0000 | 0000 0111 | |
| 3: shift Divisor right | 0000 | 0000 1000 | 0000 0111 | |
| 3 | 1: Rem = Rem − Div | 0000 | 0000 1000 | 1111 1111 |
| 2b: Rem < 0 ⇒ +Div, sll Q, Q₀ = 0 | 0000 | 0000 1000 | 0000 0111 | |
| 3: shift Divisor right | 0000 | 0000 0100 | 0000 0111 | |
| 4 | 1: Rem = Rem − Div | 0000 | 0000 0100 | 0000 0011 |
| 2a: Rem ≥ 0 ⇒ sll Q, Q₀ = 1 | 0001 | 0000 0100 | 0000 0011 | |
| 3: shift Divisor right | 0001 | 0000 0010 | 0000 0011 | |
| 5 | 1: Rem = Rem − Div | 0001 | 0000 0010 | 0000 0001 |
| 2a: Rem ≥ 0 ⇒ sll Q, Q₀ = 1 | 0011 | 0000 0010 | 0000 0001 | |
| 3: shift Divisor right | 0011 | 0000 0001 | 0000 0001 |
Quotient = 0011 = 3, Remainder = 0000 0001 = 1 ✔ 7 = 3 × 2 + 1.
6. Signed division
Same idea as signed multiply: divide the magnitudes, then fix the signs.
- Negate the quotient if the signs of dividend and divisor disagree.
- The remainder takes the sign of the dividend (when it's non-zero).
| Operation | Quotient | Remainder | Check: Q × D + R |
|---|---|---|---|
| +7 ÷ +2 | +3 | +1 | 3×2 + 1 = +7 ✔ |
| −7 ÷ +2 | −3 | −1 | −3×2 − 1 = −7 ✔ |
| +7 ÷ −2 | −3 | +1 | −3×−2 + 1 = +7 ✔ |
| −7 ÷ −2 | +3 | −1 | 3×−2 − 1 = −7 ✔ |
7. Division in MIPS
Same Hi/Lo pair as multiply, but with different contents: Lo = quotient, Hi = remainder.
| Instruction | Meaning |
|---|---|
div $s2, $s3 | Lo = $s2 / $s3, Hi = $s2 mod $s3 (signed) |
divu $s2, $s3 | unsigned quotient and remainder |
div $t3, $t2 # $t3 ÷ $t2 mflo $t1 # quotient → $t1 mfhi $t0 # remainder → $t0
Slide example — j = i % 2
int i, j; // i = $s0, j = $s1 j = i % 2; if (j == 0) i = i + 2; else i = i + 1;
addi $t0, $zero, 2
div $s0, $t0
mfhi $s1 # j = i % 2 (remainder is in Hi)
bne $s1, $zero, L1
addi $s0, $s0, 2
j L2
L1:
addi $s0, $s0, 1
L2:
...