Multiplication
Slides 26 & 27 are on your exam: the multiply algorithm flowchart and the hardware that runs it. Learn the 3 steps, then learn the 4 boxes.
1. Vocabulary
Multiplicand = the number on top (the one that gets repeatedly added).
Multiplier = the number underneath (the one whose bits we test, one at a time).
2. Paper-and-pencil binary multiplication
Exactly like decimal long multiplication, but each partial product is either a copy of the multiplicand (when the multiplier bit is 1) or all zeros (when it is 0) — because you're only ever multiplying by 1 or 0.
10101 ← Multiplicand (21)
× 101 ← Multiplier (5)
-------
10101 ← bit 0 of multiplier is 1 → copy
000000 ← bit 1 is 0 → zeros (shifted 1 left)
1010100 ← bit 2 is 1 → copy (shifted 2 left)
-------
1101001 ← Product (105) ✔ 21 × 5 = 1053. The multiplication algorithm ★ (slide 26)
The hardware can't write out partial products and add them all at the end — it has one adder. So it does the same work incrementally: initialise Product = 0, then repeat times:
- Shifting the multiplicand left is the “move the partial product one column over” step from the paper method — it multiplies it by 2.
- Shifting the multiplier right brings the next bit down into position 0, so step 1 can always just look at the rightmost bit.
4. The hardware ★ (slide 27)
Four boxes plus a control unit. For 32-bit operands:
| Register | Width | What it does |
|---|---|---|
| Multiplicand | 64 bits (2n) | Shifts left once per iteration |
| ALU | 64-bit adder | Adds Multiplicand into Product |
| Multiplier | 32 bits (n) | Shifts right once per iteration |
| Product | 64 bits (2n) | Accumulates; initialised to 0 |
In the 4-bit slide example the same picture is used with 8-bit Multiplicand / 8-bit ALU / 4-bit Multiplier / 8-bit Product — the rule is simply double the operand width.
5. Full worked trace — 0010 × 0011 (2 × 3)
4-bit operands, so 4 iterations, 8-bit registers. Follow the three steps every row.
| Iter | Step | Multiplier | Multiplicand | Product |
|---|---|---|---|---|
| 0 | Initial values | 0011 | 0000 0010 | 0000 0000 |
| 1 | 1a: bit = 1 ⇒ Prod += Mcand | 0011 | 0000 0010 | 0000 0010 |
| 2: shift Multiplicand left | 0011 | 0000 0100 | 0000 0010 | |
| 3: shift Multiplier right | 0001 | 0000 0100 | 0000 0010 | |
| 2 | 1a: bit = 1 ⇒ Prod += Mcand | 0001 | 0000 0100 | 0000 0110 |
| 2: shift Multiplicand left | 0001 | 0000 1000 | 0000 0110 | |
| 3: shift Multiplier right | 0000 | 0000 1000 | 0000 0110 | |
| 3 | 1: bit = 0 ⇒ no operation | 0000 | 0000 1000 | 0000 0110 |
| 2: shift Multiplicand left | 0000 | 0001 0000 | 0000 0110 | |
| 3: shift Multiplier right | 0000 | 0001 0000 | 0000 0110 | |
| 4 | 1: bit = 0 ⇒ no operation | 0000 | 0001 0000 | 0000 0110 |
| 2: shift Multiplicand left | 0000 | 0010 0000 | 0000 0110 | |
| 3: shift Multiplier right | 0000 | 0010 0000 | 0000 0110 |
Product = 0000 0110 = 6 ✔ 2 × 3 = 6.
6. Signed multiplication
The easy approach the book uses:
- Remember the signs of both operands.
- Convert both to positive numbers.
- Run the algorithm for 31 iterations (32-bit operands, signs left out).
- Negate the product only if the original signs disagree.
7. Multiplication in MIPS
A 32×32 product needs 64 bits, but MIPS registers are 32 bits. So MIPS keeps a dedicated pair, Hi and Lo, holding the upper and lower halves of the product.
| Instruction | Meaning |
|---|---|
mult $s2, $s3 | signed: Hi, Lo = $s2 × $s3 |
multu $s2, $s3 | unsigned product |
mflo $s1 | $s1 = Lo (move from Lo) |
mfhi $s1 | $s1 = Hi (move from Hi) |
To compute $t0 = $t2 × $t1 keeping only the low 32 bits:
mult $t2, $t1 mflo $t0
The assembler also offers a 3-operand pseudoinstruction mul $t0, $t2, $t1, which it expands into exactly the pair above.
Slide example — compiling an if/else with multiply
int i, j, m, n; // i=$s1 j=$s2 m=$s3 n=$s4 m = 2; n = 1; j = i % 2; if (j == 0) m = m * i; else n = n * i;
addi $s3, $zero, 2 # m = 2
addi $s4, $zero, 1 # n = 1
div $s1, $s3 # i ÷ 2 → Hi = remainder
mfhi $s2 # j = i % 2
bne $s2, $zero, L1 # if j != 0 jump to else
mult $s3, $s1 # m * i
mflo $s3 # m = m * i
j L2
L1:
mult $s4, $s1 # n * i
mflo $s4 # n = n * i
L2:
...Note the trick: % (remainder) is obtained with div + mfhi.