search, so that this comparison may be easily made. When this kind
of search is made, a CPI or CPD must, of course, be used as the
search is only terminated on equality.
INSERT
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
M,LTHAN
GO IF ENTRY GT KEY
JP
PO,END
NOT FOUND
ADD
HL,DE
POINT TO NEXT ENTRY
JR
LOOP
CONTINUE
LTHAN
DEC
HL
POINT TO GT ENTRY=INSERT POINT
A search is first made through the table forwards for the first value
that is greater than the search key. If no value is found greater than
the search key, then the new entry must be appended to the table
to retain the ascending order of the table. The code for this would
be at END and is not shown. If a greater than value is found, then
the pointer value in HL minus one defines the source starting ad-
dress for the move. The destination address is the length of one
entry plus the source starting address. The number of bytes to be
moved is the total number of table entries minus the current entry
multiplied by the number of bytes per entry. The move must be a
LDR to prevent overwriting data that has not yet been moved. The
actions of the INSERT are shown in Fig. 13-4. Note that this insert
is correct even if the new entry will be inserted as the first entry of
the table.
The third routine of this set modifies the key value of a table entry.
As the new value disturbs the order of the table, the table must be
reordered. One way to implement a modify of this kind would be to
AFTER
INSERT
INSERT ENTRY
TABLE
1
\ INSERTED
ENTRY
#OF
BYTES TO
MOVE NEW
LOCATION
ENTRY N
Fig. 13-4. Insert table entry actions.
THIS DATA
NOT ALTERED
201
Need help?
Do you have a question about the Z80 and is the answer not in the manual?
Questions and answers