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 77: Interrupt-Driven Printing (Spooling Data)

The Drawback of Polling Printers are extremely slow compared to the Z80 CPU. If the main program waits for the NOT BUSY status bit (Part 76) after sending every single character, the CPU spends most of its time sitting idle, waiting for the printer to finish. The Solution: Interrupt Spooling Spooling (Simultaneous Peripheral Operations On-Line) uses interrupts to feed the printer one byte at a time in the background, allowing the main program to continue executing tasks. ...

September 28, 2025

Z80 Assembly 76: Printer I/O and Parallel Data Transfer

The Challenge of Parallel Data Transfer Unlike serial communication (sending one bit at a time, Part 40), parallel communication sends an entire byte (8 bits) simultaneously across 8 dedicated data lines. This requires additional lines for handshaking to ensure the slow peripheral (the printer) is ready to receive the data. Printer Ports on the Spectrum The ZX Spectrum often uses an external interface (like the ZX Interface 1 or Kempston printer interface) to add parallel output capability. These interfaces typically map to two adjacent I/O ports: ...

September 28, 2025

Z80 Assembly 75: Low-Level Cassette Tape I/O (Saving and Loading Data)

The Cassette I/O Challenge The ZX Spectrum does not have a dedicated tape controller chip. All tape reading and writing is achieved through software routines that precisely control the timing of a single I/O line, which is one of the most demanding tasks for the Z80 CPU. The Principle: Data is encoded using Pulse Width Modulation (PWM), where the duration of the tone (the pulse width) determines whether the CPU sends a binary ‘0’ or a binary ‘1’. ...

September 28, 2025

Z80 Assembly 74: Displaying a Full Screen Image (Loading Graphics Data)

The Full Screen Data Load To display a custom image on the Spectrum (e.g., a title screen or game level background), you must copy two large blocks of data from your program’s memory into the Spectrum’s Display File RAM. The Two Blocks: Pixel Data: 6144 bytes of raw $256 \times 192$ pixel information (starts at `4000H′). Attribute Data: 768 bytes of color and flash information (starts at `5800H′). Routine 1: Loading the Pixel Data The Pixel Data is 6144 bytes long. Since the data layout is non-linear (Part 73), you generally copy the data block-by-block. However, if your source data is already arranged in the Spectrum’s non-linear format, you can use the fastest method: `LDIR′. ...

September 28, 2025

Z80 Assembly 73: The Non-Linear Screen Memory Calculation

The Challenge of the Spectrum Screen The ZX Spectrum screen memory is not linear. To save on hardware costs, the memory map was designed to simplify the display hardware’s access but complicate the CPU’s access. The Layout: The screen is divided into three major vertical bands, and within each band, the lines are interleaved. This means to find the address of the pixel at (X, Y), you must perform a complex, multi-step calculation. ...

September 28, 2025

Z80 Assembly 72: Beeper Sound and Color Control (The FEH Port)

The Combined I/O Port: FEH The ZX Spectrum uses the same I/O port address **FEH′** for multiple output functions. When you execute an OUT (FEH), A` instruction, every bit in the Accumulator (A) controls a different piece of hardware. The Output Byte Breakdown (OUT to `FEH′): Bit Function Value (If Set) Purpose 0-2 Border Color 000B - 111B Sets the color of the screen border (0=Black, 7=White). 3 Beeper Output `08H′ Toggles the speaker (0=Off, 1=On). 4 EAR Socket `10H′ Controls the tape recorder output (used for data saving/loading). 5-7 Unused N/A Ignored by the 48K Spectrum hardware. Controlling the Beeper To produce sound, you must create a fast timing loop (Part 28) that toggles Bit 3 of the output byte. ...

September 28, 2025

Z80 Assembly 71: I/O Ports and the Keyboard Matrix Scan

The Spectrum’s Key I/O Port The ZX Spectrum is minimalistic, relying on just a few I/O ports to manage all peripherals (keyboard, display, cassette, and beeper). The Main Port: The most crucial I/O address is `FEH′ (254 decimal), which is used for both input and output control. I/O Port Decoding (The Trick) The Spectrum simplifies hardware design by only connecting I/O logic to the lowest bit (A0) and the highest byte (A8-A15) of the Z80’s 16-bit address lines. ...

September 28, 2025