The Challenge: Multiplying 16-bit Numbers

The Z80 performs 8-bit addition natively (ADD), but has no instruction for 16-bit multiplication (RR×RR). When you multiply two 16-bit numbers (like HL and DE), the result is a 32-bit number. We must build this algorithm using basic operations.

The Algorithm: We use the same technique taught in grade school: break the numbers into parts, multiply each part, and sum the results. However, in assembly, it’s faster to use repeated shifting and conditional addition.

Fast 16x16-bit Multiplication (HL × DE)

This routine calculates Result (32-bit) = HL × DE. The 32-bit result will be stored across four registers (e.g., DEHL where DE is the high word and HL is the low word).

Register Pair Role
HL Multiplicand (The first number).
DE Multiplier (The second number).
BC Accumulator for the 32-bit result (High Word).
Alternate Registers (HL’, DE’, BC’) Used to hold the 32-bit accumulator (Low Word) temporarily.

The Method (Simplified): We check each of the 16 bits in the Multiplier (DE). If a bit is set, we add the Multiplicand (HL) to the result, then we shift the result left by one position.

Key Instructions Used in 16x16 Multiply

Instruction Purpose in Multiplication
ADD HL, DE The core addition of 16-bit values.
RLC R / SLA R Shifting the multiplicand left to prepare it for the next bit position.
JR C, label Checking the Carry Flag (C) after an addition to link the 16-bit result into the high 16-bit word.
DJNZ Used for the main loop, repeating the process 16 times.

Slower, but Easier to Write: For non-critical code, a much simpler approach is repeated addition: add the Multiplicand to itself ‘X’ times (where X is the Multiplier). This is compact but extremely slow.