Download Print this page

ZiLOG Z80 Handbook page 195

Hide thumbs Also See for Z80:

Advertisement

will occur. To prevent an endless loop, a simple comparison should
be made of the (i+1) index value [ (HIGH - LOW) / 2] to the ith
index value. When the two values are equal, the binary search has
ended without a match. The binary search actions of this routine for
a 15-entry table are shown in Fig. 13-7.
TABLE+O
+1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
TABEND
56
1
13
15
17
I
19
21
25
50
55
56
72
85
89
99
100
ENTRIES IN
O ASCENDING
A-D
ITERATION LOW INDEX HIGH INDEX ENTRY ACCESSED SENSE OF'MATCH
INITIALIZATION TABLE+O TABLE+14 TABLE+14 >
1
+0
+14
+7
<
2
+7
+14
+10
>
3
+7
+10
+8
<
4
+8
+10
+9
Fig. 13-7. Binary search example.
LIST OPERATIONS
When sorted data must be altered frequently and entries deleted,
inserted, or modified, the overhead of altering data tables becomes
significantly large. A list is a data structure that reduces the over-
head by eliminating movements of large blocks of data when items
are changed within the list. A single-ended list consists of entries that
are noncontiguous in memory. Each entry consists of the data as-
sociated with that list entry and a pointer (address) to the next data
items in the list. As the data items are linked by the address pointers,
the entries in the list may be in any order in memory. The head of
the list is referenced by a variable in memory. The last item in the
list often has a -1 or other invalid address to signify that it is
the last item. Fig. 13-8 shows a typical single-ended linked list of
nine data items with four bytes per entry. The last two bytes are the
pointer to the next item in the list. The head of the list is referenced
by HEADLS.
204

Advertisement

loading
Need help?

Need help?

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

Questions and answers