cpir

Compare, increment and repeat

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

2 bytes21/16 cycles21 cycles each time it repeats, 16 the last time

SZHP/VNC ●●●●1–
  • ● depends on the result
  • 1 always 1
  • – 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.

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