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 26: Collision Detection (Bounding Boxes and Bitwise Checks)

Bounding Box Collision (The Quick Check) Collision detection is computationally expensive. The fastest method is the Bounding Box check. This method checks if the rectangular area occupied by two objects (their boxes) overlap on the X and Y axes. If the boxes don’t overlap, the objects cannot be touching. The Principle: An overlap exists if: Object A’s right edge (A.X + A.Width) is > Object B’s left edge (B.X), AND Object A’s left edge (A.X) is < Object B’s right edge (B.X + B.Width), AND The same two conditions are met for the Y-axis. Z80 Implementation (X-Axis Check): ...

September 27, 2025

Z80 Assembly 05: Logic Operations and Bit Manipulation

Logic Operations: AND, OR, and XOR These instructions perform bitwise logic operations between the Accumulator (A) and a source (another register or immediate value), storing the result back in A. They are crucial for filtering or combining specific data flags. The Core Logic Commands: Instruction Action Purpose AND N Bitwise AND (A = A & N) Filtering/Masking: Clears (forces to 0) any bits in A that are 0 in N. OR N Bitwise OR (A = A | N) Setting: Sets (forces to 1) any bits in A that are 1 in N. XOR N Bitwise XOR (A = A ^ N) Toggling: Flips the state (0→1, 1→0) of any bits in A that are 1 in N. Example: Filtering a Status Register If we only care about the highest two bits of a status register read into A: ...

September 27, 2025

Z80 Assembly 02: Arithmetic, Flags, and Control Flow

Arithmetic and the Flags Register Operations like ADD and SUB affect the Flags Register (F), which is essential for conditional execution. Key Flags: Z (Zero): Set if the result of an operation is zero. C (Carry): Set if there is a carry-out from the most significant bit. Instruction Description ADD A, R Adds the 8-bit contents of R to A. Only A can be the destination. ADD HL, DE Adds the 16-bit contents of DE to HL. Controlling Execution with Jumps Jumps (changing the Program Counter) allow programs to make decisions and repeat blocks of code. ...

September 27, 2025