CPIR
Exchange, Block Transfer and Search Group
Expansion:
ComPare and Increment Repeated
Usage:
cpir
The CPIR (Compare and Increment Repeated) instruction in Z80 assembly is used to compare an 8-bit value at the address in HL with the accumulator, then, increment the HL register pair, and decrement the BC register pair. This is useful for searching through a block of memory for a specific value.
This instruction is similar to CPI except that the operation is repeated until either BC is zero or a match is found (A == (HL)).
| Instruction | Description | Opcode |
|---|---|---|
| CPI | ComPare and Increment Repeated | EDB1 |
Example
; Example
ld a, $42 ; load the value to compare into the accumulator (let's say we're looking for $42)
ld hl, $4000 ; load the starting address of the memory block into HL
ld bc, $1000 ; load the length of the memory block into BC (4096 bytes)
cpir ; compare (HL) with A, increment HL, and decrement BC
jp nz, NotFound ; jump to NotFound if not equal and BC == 0
jp z, Found ; jump to Found if a match is found (A == (HL))
NotFound:
; handle case where the value was not found
nop ; no operation (placeholder for not found code)
Found:
; handle case where the value was found
nop ; no Operation (placeholder for found code)