# `cpir`

[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/cpir

**Compare, increment and repeat**

Category: Block and string

Repeats CPI, scanning up memory from HL for a byte equal to A, until a match is found or BC is zero.

2 bytes · 21/16 cycles · 21 cycles each time it repeats, 16 the last time

## Flags

| Flag | Effect |
| --- | --- |
| S | depends on the result |
| Z | depends on the result |
| H | depends on the result |
| P/V | changed as the description says |
| N | always 1 |
| C | not changed |

## Forms

- `cpir` · Compares the value of the memory location pointed to by HL with A. Then increments HL and decrements BC. Repeats this operation until BC is zero or the zero flag is set. Resets the parity/overflow flag if BC becomes zero and sets it otherwise, so a set flag shows that HL reached a memory location whose value equalled A before the counter went to zero. Interrupts can trigger while this instruction is processing.

## Example

```z80
        .org 0x8000
        ld a, 3
        ld hl, source
        ld bc, 4
        cpir
        halt
source: .byte 1, 2, 3, 4
```
