ZX Spectrum's CPU couldn't do multiplication
Today I Learned that programming on the ZX was harder than I imagined. The Z80 processor
wasn’t exactly a state-of-the-art design when the ZX launched, but I never anticipated that it didn’t even come with something I take for granted today:
a simple multiply
instruction.
The naive approach was to make additions inside a loop, but there’s a clever technique
that takes that multiplication algorithm we learned back in school
and applies it to binary digits. Since anything times 0
is 0 and times 1
is itself, this approach doesn’t need a
pre-computed multiplication table like we did in school. It also can save some loops by using bitwise operations. Clever.
An example: Multiplying 26 (11011
) by 12 (1100
)
11011
× 1100
————————————————
00000
00000∙
11011∙∙
+ 11011∙∙∙
————————————————
101000100
Result: 324 (101000100
)
Other computers from that era also had to resort to these techniques, some because their CPU also lacked a multiply
instruction
(Commodore 64 [MOS 6510],
Apple II [MOS 6502]) and other because the CPU instruction for
multiplication was slower than other procedural division algorithms such as the
IBM PC (Intel 8086) — in the case of x86, however, the
faster algorithm relied on a pre-computed memory table, thus trading memory for speed; see the linked article.