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 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 50: Floating-Point to ASCII Conversion

The Challenge of Displaying Floats In Z80 assembly, a floating-point number is stored internally across multiple bytes (e.g., 4 or 5 bytes) in a specialized format (Part 30). The screen, however, only displays ASCII characters. Converting the binary float into a series of characters (“3”, “.”, “1”, “4”) is one of the most complex tasks in 8-bit programming. The Conversion Strategy The process is too complex for a single routine and is typically broken down into four major steps: ...

September 27, 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