Download Print this page

ZiLOG Z80 Handbook page 186

Hide thumbs Also See for Z80:

Advertisement

an "e" in an alphanumeric string), then a significant amount of time
will be wasted in comparing the remainder of the string to the search
string; if the first character of the search string occurs less frequently
(such as a "q"), then the number of "failing comparisons" will be
fewer and the overall search more efficient. One general way to
search for a given string of more than one character is given in the
next sample instructions. Here, a search is made for a two-character
string. The first level of the search is performed for the first letter
of the key string. When the first letter is found in the string to be
searched, the next character is compared to the second key character.
If a match is made, the routine exits; but if a match is not made, the
routine restarts from the point at which the first character was found.
Here, the search is through a string of 64 characters. The search is
over if no match has been found by the 63rd character.
BIGSRC
LD
HL,STRING
START OF STRING
LD
BC,63
63 BYTES MAXIMUM
LOOP
LD
A,(CHAR+O)
FIRST CHARACTER OF KEY
CPIR
SEARCH FOR 1ST CHAR
JP
NZ,NFND
GO IF NOT FOUND
SECLVL
LD
D,(HL)
GET SECOND CHAR
LD
A,(CHAR+1)
GET SECOND CHAR OF KEY
CP
A,D
COMPARE
JP
NZ,LOOP
GO IF NOT FOUND
FOUND
HL POINTS TO MATCH + 1
If the first key character has a match , then the second level com-
parison at SECLVL is entered . HL at this point points to the match
plus one so that the next character can be picked up directly. A is
then loaded with the second key character and a comparison made.
If there is a compare , FOUND is executed; if there is no compare,
LOOP is reentered . The BC and HL registers are already properly
set for the next CPIR comparison in the no match case!
A variation of the above technique can be used to search for a
given length key or a hashing comparison may be made for the sec-
ond level compare. In the latter case, a one-for-one compare of each
of the remaining bytes in the key and string is not made. The re-
maining bytes of the string are used to compute a hash value which
is then compared with a precomputed key hash value. If the two
hash values are equal, then a direct compare is made. If the two
hash values are not equal, then the search continues . The hash
algorithm may be any scheme that produces a relatively unique
value. If the hash algorithm adds the remaining bytes together, for
example, a relatively unique hash value is found as shown in Table
13-1. Here, about 1 in 30 five-character strings will produce the same
hash value, as is the case for "BROWN" and "MAUVE."
195

Advertisement

loading