Z80 Assembly 90: 16-bit Integer Division (Quotient and Remainder)

The Challenge of Integer Division The Z80 CPU has no instruction for division. Every division operation must be performed using software algorithms based on repeated subtraction or shifting and subtraction (the same method used for manual long division). Goal: Divide a 16-bit number (the Dividend) by an 8-bit number (the Divisor) to produce a 16-bit Quotient and an 8-bit Remainder. The Algorithm: Binary Long Division The most efficient method is to implement the Binary Long Division algorithm, which uses shifts and conditional subtraction. ...

September 28, 2025

Z80 Assembly 87: 16-bit Comparison (Greater Than, Less Than, Equal)

The Need for 16-bit Comparison The Z80 has a dedicated CP R&prime; (Compare) instruction, but it only works on 8-bit values. When dealing with addresses, large scores, or multi-byte numbers, you need a robust routine to compare two 16-bit pairs, such as HL′ against `DE′. Goal: Compare HL with DE and set the flags (Zero, Carry) to indicate if HL &equals; DE&prime;, HL < DE′, or `HL > DE′. The Comparison Strategy: Subtraction The fastest way to compare two numbers is to subtract one from the other. ...

September 28, 2025

Z80 Assembly 84: 16-bit Binary to BCD Conversion

The Challenge of Displaying Numbers Once the Z80 has calculated a final value (e.g., a score of 4296 decimal), this number is stored as a 16-bit binary value (1000010000000000B). To display it on the screen, we need to convert it into a sequence of decimal digits (4&prime;, 2′, 9&prime;, 6′) that can be looked up in the font table. Goal: Convert a 16-bit binary number into a 16-bit BCD value (four decimal digits). ...

September 28, 2025

Z80 Assembly 83: 16-bit BCD to Binary Conversion

The Challenge of User Input When a user types a number (e.g., 4296), it is stored as a series of ASCII characters (or often converted immediately into Binary-Coded Decimal (BCD)). The Z80 needs this number in a single 16-bit binary value for fast calculation. Goal: Convert a 16-bit BCD value (representing a number from 0 to 9999) into a 16-bit binary value. The BCD Structure A 16-bit BCD value typically uses the entire word (two bytes) to store four decimal digits (0-9). ...

September 28, 2025

Z80 Assembly 82: Fixed-Point Arithmetic (Fast Fractions)

The Need for Fixed-Point Math As established (Part 30), Z80 floating-point math is extremely slow. Fixed-point arithmetic is the solution: it allows the Z80 to perform calculations involving fractions using only fast integer operations. The Principle: A fixed-point number is an integer where the position of the decimal point is implied and fixed by the programmer. The Q-Format (Standard Fixed-Point) We represent a fractional number (like 3.14) by scaling it up to make it an integer. This is often called a Q-format (e.g., Q16, Q8). ...

September 28, 2025

Z80 Assembly 81: Floating-Point Multiplication/Division by Powers of Two

The Optimization: Exploiting the Format In standard integer math, multiplying by a power of two is done with a fast left shift (`SLA′). In floating-point math (Part 30), a multiplication or division by a power of two (like $2^N$ or $2^{-N}$) is even faster: it requires no change to the Mantissa (precision) and only a simple addition or subtraction to the Exponent. The Rule: Multiplication by $2^N$: Add $N$ to the Exponent. Division by $2^N$: Subtract $N$ from the Exponent. The Exponent Field Recall that the exponent is usually stored as a biased 8-bit integer (e.g., a bias of 127 is added to the true exponent). Adjusting the number simply means adjusting this byte. ...

September 28, 2025

Z80 Assembly 80: Floating-Point Square Root (Newtons Method)

The Challenge: Precision and Speed Calculating the square root of a floating-point number (Part 30) is required for precise calculations like distance, physics, or graphics shading. This requires an iterative algorithm that works across the multi-byte structure of the float. Method: The Newton-Raphson Method is the preferred algorithm for floating-point square roots because it is computationally fast (it converges quadratically). The Newton-Raphson Formula The goal is to iteratively refine a guess ($x_{n+1}$) using the previous guess ($x_n$) until the guesses stop changing (convergence). ...

September 28, 2025

Z80 Assembly 79: Square Root Calculation (Newton's Method vs. Binary Search)

The Challenge of Square Roots The Z80 cannot natively calculate a square root. This complex operation must be achieved using an iterative algorithm that repeatedly guesses the answer and refines the guess until it is accurate enough. Goal: To find the largest integer $R$ such that $R^2 \le N$, where $N$ is the input number. Method 1: Integer Binary Search (The Reliable Method) The Binary Search method is the safest and most reliable way to find the integer square root on an 8-bit processor. It works by checking the midpoint of a search range. ...

September 28, 2025

Z80 Assembly 78: Software Floating Point Division

The Complexity of Division While multiplication (Part 16) is complex, floating-point division is arguably the most difficult operation to implement in Z80 assembly. It combines the complexity of multi-byte arithmetic with the iterative processes of long division, applied across three components: Sign, Exponent, and Mantissa. The Formula (Conceptual): $$\text{Result}_{\text{Sign}} = \text{Operand}_A.\text{Sign} \text{ XOR } \text{Operand}B.\text{Sign}$$ $$\text{Result}{\text{Exponent}} = \text{Operand}_A.\text{Exponent} - \text{Operand}B.\text{Exponent}$$ $$\text{Result}{\text{Mantissa}} = \text{Operand}_A.\text{Mantissa} / \text{Operand}_B.\text{Mantissa}$$ Step 1: Sign and Exponent Calculation The sign of the result is determined by a simple XOR operation on the two input sign bits (Part 8). ...

September 28, 2025

Z80 Assembly 30: Emulating Floating-Point Arithmetic (High-Level Math)

The Challenge of Floating-Point The Z80 is an integer processor—it can only handle whole numbers. Floating-point numbers (like 3.14159 or 1.2e-5) are necessary for high-precision math used in physics simulations, graphics transformations, or advanced financial calculations. To use them, we must emulate the standard IEEE 754 format using multiple bytes of memory. Floating-Point Structure (The Four-Byte Standard): A typical 32-bit (4-byte) single-precision float is stored across four consecutive memory locations: ...

September 27, 2025