Download Print this page

ZiLOG Z80 Handbook page 190

Hide thumbs Also See for Z80:

Advertisement

value TABLE + (N * 128) points to the last byte of the table plus
one, so one is subtracted from the expression to point to the last ID
# in the table. A search is then made, one ID at a time, by a CPD.
If a match is not found, 127 is decremented from HL to point to the
next ID (the HL register pair has already been decremented once
by the CPD). If the ID # is not in the table, BC is decremented
down to zero and the NFND routine is entered.
The preceding routine shows a search for a 1-byte key in a table
of entries. Searches for n-byte keys may be made using the same
methods to sequence through a table, forward or backward, and
may use the string comparison techniques described in previous
examples.
When it becomes necessary to order tables, the block transfer rou-
tines may be used to advantage. Although it is good programming
practice not to move large blocks of data from one set of locations in
memory to another, at times data must be inserted in tables, deleted
from tables, or the key value must be modified and the table re-
ordered. In the following routines, the general parameters are
TABLE, the starting address of the table, NENT, the number of
entries in the table, and LENT, the length in bytes of each entry.
The address of the last word in the table + 1 is given by LASTW.
The inputs to the multiply routine are in the A register (multipli-
cand) and B register (multiplier). The output of the multiplier rou-
tine is a 16-bit product in the BC register. The key for the search
is always assumed to be in the first byte of the table entry.
The first routine in this set deletes an entry from the table. A
search is first made to find the entry by a CPI. When the entry is
found, the entries below the delete entry are moved up by an LDIR
instruction. Note that this instruction must be an LDIR as an LDDR
would overwrite data to be moved as the move was implemented.
Finally, the number of entries in the table are decremented by one.
DELETE LD HL,TABLE START OF TABLE
LD BC,NENT # OF ENTRIES IN TABLE
LD A,KEY SEARCH KEY
LD DE,LENT # OF BYTES PER ENTRY
DEC DE # OF BYTES PER ENTRY - 1
LOOP CPI SEARCH ONE ENTRY
JP Z,FOUND GO IF FOUND
JP PO,NFND NOT FOUND!
ADD HL,DE POINT TO NEXT ENTRY
JP LOOP CONTINUE
FOUND DEC HL POINT TO 1ST BYTE OF DELETE
PUSH HL
LD HL,LASTW LAST WORD + 1
OR A CLEAR C
199

Advertisement

loading
Need help?

Need help?

Do you have a question about the Z80 and is the answer not in the manual?

Subscribe to Our Youtube Channel