CPD
Exchange, Block Transfer and Search Group
Expansion:
ComPare and Decrement
Usage:
cpd
The CPDR operation compares the contents of the memory location addressed by the HL register pair with the Accumulator (A). After the comparison, the HL and Byte Counter (BC) Register pairs are decremented.
Multi-function instruction which performs the following actions in order.
- CP (HL)
- DEC HL
- DEC BC
| Instruction | Description | Opcode |
|---|---|---|
| CPD | Multi-function instruction | EDA9 |
Example
; Example
ld a, $42 ; load the value to compare into the accumulator
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
SearchLoop:
cpd ; compare (HL) with A, decrement 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)