Home

Z80  Programming  IV-a

RAM - Filling a memory block with a single data byte

Page a

How LDIR works

This is the traditional use of the LDIR command:

  LD HL,     $0000    ; Pointer to the source.

  LD DE,     $8000    ; Pointer to the destination.

  LD BC,     $4000    ; Number of bytes to move.

  LDIR                      ; Copies BC bytes from (HL) to (DE). BC is decremented as each byte from HL location is copied to DE location, and then HL and DE are incremented.

 

Here's what page 132 of the Z80 CPU Manual has to say about the LDIR instruction:

"This 2-byte instruction transfers a byte of data from the memory location addressed by the contents of the HL register pair to the memory location addressed by the DE register pair.

Both these register pairs are incremented and the Byte Counter (BC) Register pair is decremented."

 

LDIR performs the equivalent of the following;

START:

            LD (DE),  (HL)     ; Copy contents of address HL to address DE.

            INC HL                ; Increment HL register.

            INC DE                ; Increment DE register.

            DJNZ  START       ; Decrement counter BC. If not zero, jump to START.

   

To demonstrate the program works, we ran it through OshonSoft Z80 Simulator IDE software. We reduced the values of all 3 registers as you can see in the .ASM file below left. In the bottom part of the .ASM file is the equivalent of the .LST listing file that TASM produces.  (Note that this Assembler prefers hexadecimal numbers to end in "h" and not started with "$" as we saw with TASM.)

We're essentially copying $FF from location $A0 to location $AF, inclusive. That is shown in the adjacent Memory Editor screen.

       

 

When the Simulator completes, it provides an execution listing of the program. You can see starting in line 5 below that all that the Z80 does is run LDIR fifteen times once the three registers are "primed" for action.

Coloured arrows are showing the contents of the BC, DE, and HL register pairs after each instruction's execution.

 

We could use the program above to copy our ROM to RAM, couldn't we?

 

Summary

 - We created source code to use Z80 instruction "LDIR" to modify a block of code instead of just copying it

 - We demonstrated the value in having a Z80 IDE like OshonSoft's Simulator to pre-test your source code or a portion of it

 - Later we'll show you how to download and configure the 30-hour free-use copy of the OshonSoft Z80 Simulator IDE application

 

TOP

 

Home