The Need for Block Operations
In system programming and game development, you constantly need to move large chunks of data—transferring level data, shifting arrays, or buffering screen regions. Writing a loop for this is slow; the Z80 provides dedicated instructions for speed.
Routine 1: Block Move (memcpy)
`memcpy′ (Memory Copy) copies a block of bytes from a source address to a destination address. This is the fastest way to achieve the operation.
Key Instruction: The **LDIR′** (Load, Increment, Repeat) instruction (Part 7) is the core of
memcpy′.
Setup and Execution:
Register Pair | Purpose |
---|---|
HL | Source Address (Where to read data from) |
DE | Destination Address (Where to write data to) |
BC | Counter (The total number of bytes to move) |
MEMCPY_ROUTINE:
; Assume parameters are passed in HL, DE, and BC
LDIR ; Copies [BC] bytes from (HL) to (DE), increments all three
; Critical Check: Overlapping Memory
; LDIR correctly handles non-overlapping blocks. If the source and destination
; blocks overlap, you must use `LDDR′ (Load, Decrement, Repeat) if the destination
; address is LOWER than the source address, to prevent data corruption.
RET
Routine 2: Block Swap (memswap)
`memswap′ exchanges the contents of two memory blocks of the same length (e.g., swapping the user stack area with a backup stack). This is a complex operation that cannot be done with a single instruction.
The Strategy: You must use a third temporary buffer in RAM as an intermediary storage area.
The Steps (Simplified):
- Allocate Scratchpad: Designate a scratchpad area in RAM (size $\ge$ block length).
- Move A to Scratchpad: `memcpy′ the contents of Block A to the scratchpad.
- Move B to A: `memcpy′ the contents of Block B to Block A.
- Move Scratchpad to B: `memcpy′ the contents of the scratchpad to Block B.
Z80 Implementation Focus: This routine calls the MEMCPY_ROUTINE′ three separate times, adjusting the HL, DE, and BC registers for each phase of the exchange. All three calls rely on the speed of the
LDIR′ instruction.
Efficiency Trade-off: While fast, `memswap′ requires reserving a scratchpad buffer in RAM equal to the size of the blocks being swapped.