Download Print this page

ZiLOG Z80 Handbook page 184

Hide thumbs Also See for Z80:

Advertisement

be done "semi-automatically," that is, one instruction will be exe-
cuted. The byte pointed to by HL will be accessed and compared to
the contents of the A register. The contents of HL will be incre-
mented and the contents of BC will be decremented. If BC 0 after
the decrement, the P/V flag will be set. The S, Z, and H (half-carry)
will be set if the comparison result is negative, equal, or produces a
half-carry, respectively. The next instruction in sequence will then
be executed. Examples of CPI use will be presented under table
operations in this chapter.
If the instruction is a CPIR instruction, all of the setup and execu-
tion details are the same, except that the CPIR will continue execu-
tion until either a match is found with the search key in the A regis-
ter or until the byte count has been decremented down to zero
(BC = 0). Testing of the P/V and Z flags will indicate the terminat-
ing condition.
As an example of CPIR use, consider the following example. A
string of 64 characters, starting at STRING, is to be searched for the
character "$."
SRCHD LD A,24H DOLLAR SIGN
LD HL,STRING ADDRESS OF FIRST CHAR
LD BC,64 64 BYTES MAXIMUM
CPIR SEARCH STRING FOR $
JP Z,FOUND GO IF CHARACTER FOUND
NOT FOUND
FOUND DEC HL POINT TO $
LD (PNTR),HL SAVE POINTER TO $
The A register is loaded with the hexadecimal equivalent of an
ASCII dollar sign. Register pair HL is initialized with the address of
the start of the character string, STRING. Since 64 characters are
to be searched, a byte count of 64 is loaded into BC. The CPIR se-
quences through the 64-character string. If a dollar sign is found,
CPIR is exited to the next instruction with the Z flag set. If the
dollar sign was at the last character, both the Z flag and the P/V
flag will indicate terminating conditions; the Z flag will be set indi-
cating the character was found and the P/V flag will be reset indi-
cating that the byte count was decremented down to zero. It is neces-
sary to test the Z flag first, therefore, to see if the character was found
before testing the P/V for end of string. If the character is found, the
HL register points to the byte after the character, so the pointer
must be adjusted by one to point to the location of the dollar sign.
The terminating conditions for a typical successful search for this
example are given in Fig. 13-1.
As described in a previous chapter, the CPDR works in similar
fashion to the CPIR, except that it decrements from the end of the
193

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