High-Speed Block Transfer Instructions
The Z80 includes a powerful set of instructions designed to handle large blocks of memory quickly. These commands primarily rely on three 16-bit register pairs:
Register Pair | Purpose in Block Commands |
---|---|
HL | Source Address (Where to read data from) |
DE | Destination Address (Where to write data to) |
BC | Counter (How many bytes to move/compare) |
Transferring Data (Move)
The LDI (Load, Increment) and LDIR (Load, Increment, Repeat) commands are used to copy blocks of memory.
Instruction | Action | Description |
---|---|---|
LDI |
1. Copy byte: (DE) ← (HL) 2. Increment: HL++ , DE++ 3. Decrement: BC-- |
Copies one byte and prepares for the next. |
LDIR |
Repeats LDI until BC = 0 . |
Copies the entire block defined by BC, very fast! |
Example: Copying a 1 KB Block
LD HL, SOURCE_ADDR ; Start of data to copy
LD DE, DEST_ADDR ; Location to paste data
LD BC, 1024 ; 1 KB (Number of bytes)
LDIR ; Execute the high-speed copy
Searching and Comparing Data
The CPI (Compare, Increment) and CPIR (Compare, Increment, Repeat) instructions are used to search for a specific byte value or compare two blocks. The value to search for is always held in the Accumulator (A).
Instruction | Action | Description |
---|---|---|
CPI |
1. Compare: A - (HL) 2. Increment: HL++ 3. Decrement: BC-- |
Compares A with the byte at HL and sets the Zero flag (Z) if they match. |
CPIR |
Repeats CPI until BC = 0 OR the Zero flag (Z) is set. |
Searches the entire block. Stops the moment a match is found. |
Example: Searching for a Null Terminator (00H)
LD A, 0 ; Load the null character (0) to search for
LD HL, STRING_START ; Address of the string
LD BC, 0FFFFH ; Search entire 64KB (BC = search length)
CPIR ; Search until the first 00H is found
; If Z=1, the byte was found at HL-1 (HL is auto-incremented)