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:

    IN   A, (STATUS_PORT) ; Read 8-bit status into A
    AND  11000000B        ; Use a binary mask (C0H)
    ; Now A contains only the state of the two highest bits.

The Power of Individual Bit Commands

The Z80 has dedicated instructions for manipulating specific bits without affecting the others. This is one of the Z80’s greatest strengths.

The BIT/SET/RES Block:

Instruction Action Analogy
BIT B, R Test bit B (0-7) in register R. Sets the Zero flag if the bit is 0. Ask, “Is this specific switch turned off?”
SET B, R Set bit B in register R to 1. Flip this specific switch ON.
RES B, R Reset bit B in register R to 0. Flip this specific switch OFF.

Example: Testing a Flag

    ; Test bit 5 in register C
    BIT  5, C
    JP   Z, BIT_IS_ZERO   ; Jump if bit 5 of C is 0

Example: Setting and Resetting Flags

`&grave