CPI
Exchange, Block Transfer and Search Group
Expansion:
ComPare and Increment
Usage:
cpi
The CPI (Compare and Increment) 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.
| Instruction | Description | Opcode |
|---|---|---|
| CPI | ComPare and Increment | EDA1 |
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)
SearchLoop:
cpi ; compare (HL) with A, increment HL, and decrement BC
jp nz, SearchLoop ; jump to SearchLoop 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)