Z80 Assembly 16: Efficient 16-bit Multiplication Algorithm

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. ...

September 27, 2025

Z80 Assembly 08: Multi-Byte Math and the Alternate Registers

Arithmetic with Carry: ADC and SBC When adding numbers larger than 8 bits (like a 16-bit word or a 32-bit integer), you must add the Carry flag (C) from the previous operation into the current one. This is done with the ADC and SBC instructions. The Principle: Add (or Subtract) the current byte PLUS the Carry flag. Instruction Action Purpose ADC A, R A ← A + R + C Add with Carry: Used to link the result of the previous 8-bit addition. SBC A, R A ← A - R - C Subtract with Carry/Borrow: Used to link the result of the previous 8-bit subtraction. Example: 16-bit Addition (BC + DE) ...

September 27, 2025

Z80 Assembly 06: Rotation and Shifts (Fast Multiplication/Division)

Understanding Shifts and Rotations Shift and rotate instructions move the bits within a register. This is often done to perform arithmetic: shifting a number left by one position is equivalent to multiplying by 2, and shifting right is equivalent to integer dividing by 2. Key Difference: Shift: The bit that leaves one end is discarded, and a new bit (0 or the sign bit) enters the other end. Rotate: The bit that leaves one end wraps around to enter the other end, forming a circle. Arithmetic Shifts (SLA and SRA) Arithmetic shifts are best for multiplication and division because they handle the sign of the number correctly. ...

September 27, 2025