If the IY register was to be used instead of the IX, the instruction
format would be virtually identical, with "IY" substituted for "IX."
The index register-oriented instructions can be used to advantage
for moving data as in the following routine that moves the three
bytes in BLK2 through BLK2 + 2 to BLK1 through BLK1 + 2. The
move is implemented in reverse fashion starting at BLK2 + 2 and
BLK1 + 2. IX holds the source pointer while IY holds the destination
pointer. Both index registers are decremented by a DEC IX or DEC
IY instruction.
NXT1
LD
LD
LD
IX,BLK2 + 2
IY,BLK1 + 2
B,(IX)
INITIALIZE START OF SOURCE
INITIALIZE START OF DEST
SAME AS (IX + 0)
LD
(IY),B
SAME AS (IY + 0)
DEC IX
DEC IY
POINT TO NEXT BYTE SOURCE
POINT TO NEXT BYTE DEST
NXT2
LD
B,(IX)
NEXT
LD
(IY),B
DEC IX
DEC IY
NXT3
LD
B,(IX)
NEXT
LD
(IY),B
Code such as the above is inefficient in memory storage because
the same basic operation is repeated many times. The transfers at
NXT1, NXT2, and NXT3 are almost identical . If 100 bytes were to
be transferred, it would of course be ludicrous to repeat the identi-
cal actions 100 times. The most efficient way to implement repetitive
actions is by looping back to the same set of instructions for as many
times, N , as required. This is done in the following program which
uses IX and IY as source and destination pointers as before and
moves 100 bytes from BLK2 to BLK1. The initial count, N = 100,
is held in the C register and is decremented down to 0. The loop at
LD IX,BLK2 STRT OF SRC BLK
EXECUTED ONCE ONLY LD IY,BLK1 STRT OF DST BLK
LD C,100 SET N = 100
LOOP LD B ,(IX) LOAD BYTE
LD (IY),B STORE BYTE
100 INC IX BMP IND BY ONE
I
TIMES
LDONE
INC IY
DEC C N - 1
JP NZ,LOOP GO IF NT DN (Z=1)
DONE HERE
149
Need help?
Do you have a question about the Z80 and is the answer not in the manual?