# `xor`

[Z80 Documentation](https://asm-editor.specy.app/documentation/z80.md) › [Instructions](https://asm-editor.specy.app/documentation/z80/instruction.md)

Web page: https://asm-editor.specy.app/documentation/z80/instruction/xor

**Logical exclusive OR**

Category: Logic

Sets A to the bitwise exclusive OR of A and a register, memory byte or immediate value.

Used in: [8-bit and 16-bit arithmetic, logic and bits](https://asm-editor.specy.app/learn/courses/z80/think-in-assembly/arithmetic-logic-and-bits.md) (Z80 assembly), [The screen, keyboard and mouse through ports](https://asm-editor.specy.app/learn/courses/z80/talking-to-the-outside-world/screen-keyboard-and-mouse.md) (Z80 assembly), [Sum of an array](https://asm-editor.specy.app/learn/courses/z80/examples/sum-of-an-array.md) (Z80 assembly), [Multiply and divide, with the remainder](https://asm-editor.specy.app/learn/courses/z80/examples/multiply-and-divide.md) (Z80 assembly), [A jump table](https://asm-editor.specy.app/learn/courses/z80/examples/jump-table.md) (Z80 assembly), [Convert a number to a string in any base](https://asm-editor.specy.app/learn/courses/z80/examples/number-to-string.md) (Z80 assembly), [Drawing shapes on the screen](https://asm-editor.specy.app/learn/courses/z80/examples/drawing-on-the-screen.md) (Z80 assembly), [A bouncing ball](https://asm-editor.specy.app/learn/courses/z80/examples/bouncing-ball.md) (Z80 assembly), [Move a square with the keyboard](https://asm-editor.specy.app/learn/courses/z80/examples/keyboard-control.md) (Z80 assembly), [The snake game](https://asm-editor.specy.app/learn/courses/z80/examples/snake.md) (Z80 assembly)

## Flags

| Flag | Effect |
| --- | --- |
| S | depends on the result |
| Z | depends on the result |
| H | always 0 |
| P/V | holds the parity of the result |
| N | always 0 |
| C | always 0 |

## Forms

- `xor r` · 1 byte · 4 cycles · Sets A to the bitwise XOR of A and `r`.
  - `xor b`
  - `xor c`
  - `xor d`
  - `xor e`
  - `xor h`
  - `xor l`
  - `xor a`

- `xor (hl)` · 1 byte · 7 cycles · Sets A to the bitwise XOR of A and (HL).

- `xor nn` · 2 bytes · 7 cycles · Sets A to the bitwise XOR of A and `nn`.

- `xor (ix+dd)` · 3 bytes · 19 cycles · Sets A to the bitwise XOR of A and the value pointed to by IX plus `dd`.
  - `xor (ix+dd)`
  - `xor (iy+dd)`

## Pseudo-instructions

The assembler also accepts these forms, which are other ways to write Z80 instructions. Open a row to see what each form becomes.

- `xor a, r` · 1 byte · 4 cycles · Sets A to the bitwise XOR of A and `r`.
  - `xor a, b` → `xor b`
  - `xor a, c` → `xor c`
  - `xor a, d` → `xor d`
  - `xor a, e` → `xor e`
  - `xor a, h` → `xor h`
  - `xor a, l` → `xor l`
  - `xor a, a` → `xor a`

- `xor a, (hl)` · 1 byte · 7 cycles · Sets A to the bitwise XOR of A and (HL).
  - `xor a, (hl)` → `xor (hl)`

- `xor a, nn` · 2 bytes · 7 cycles · Sets A to the bitwise XOR of A and `nn`.
  - `xor a, nn` → `xor nn`

- `xor a, (ix+dd)` · 3 bytes · 19 cycles · Sets A to the bitwise XOR of A and the value pointed to by IX plus `dd`.
  - `xor a, (ix+dd)` → `xor (ix+dd)`
  - `xor a, (iy+dd)` → `xor (iy+dd)`

## Undocumented

Zilog’s manual does not list these forms, but the Z80 runs them.

- `xor ixh/l` · 2 bytes · 8 cycles · Sets A to the bitwise XOR of A and `ixh/l`.
  - `xor ixh`
  - `xor ixl`
  - `xor iyh`
  - `xor iyl`

## Pseudo-instructions · Undocumented

The assembler also accepts these forms, which are other ways to write Z80 instructions. Open a row to see what each form becomes. Zilog’s manual does not list these forms, but the Z80 runs them.

- `xor a, ixh/l` · 2 bytes · 8 cycles · Sets A to the bitwise XOR of A and `ixh/l`.
  - `xor a, ixh` → `xor ixh`
  - `xor a, ixl` → `xor ixl`
  - `xor a, iyh` → `xor iyh`
  - `xor a, iyl` → `xor iyl`

## Operands

- `r`: An 8-bit register: `a`, `b`, `c`, `d`, `e`, `h` or `l`.
- [`(hl)`](https://asm-editor.specy.app/documentation/z80/registers/operand-hl.md): The byte in memory at the address stored in `hl`.
- [`nn`](https://asm-editor.specy.app/documentation/z80/registers/operand-nn.md): An 8-bit immediate value, `0` to `255` or `-128` to `127`, written as a number, character constant or symbol.
- [`(ix+dd)`](https://asm-editor.specy.app/documentation/z80/registers/operand-ix-dd.md): The byte in memory at `ix + dd`, where `dd` is a signed displacement from `-128` to `127`, for example `(ix+5)` or `(ix-3)`. `(iy+dd)` works the same way.
- [`ixh/l`](https://asm-editor.specy.app/documentation/z80/registers/register-ixh.md): The high or low byte of `ix`: `ixh` or `ixl`. For `iy`, use `iyh` or `iyl`.

## Example

```z80
        .org 0x8000
        ld a, 5
        ld b, 3
        xor b
        halt
```
