The AND instruction can be used to mask in, or mask out, un-
wanted fields within data bytes. Suppose, for example, that an 8-bit
byte in memory holds two packed bed digits, one in bits 7-4 and the
other in bits 3-0. It is necessary to test the second bed digit. The
first bed digit would be masked out and the second bed digit would
remain for testing by the following code.
LD A,(DIGITS) GET 2 BCD DIGITS
AND OFH MASK OUT HIGH ORDER
TEST
Since bits 7-4 of the immediate data value were 0, the correspond-
ing bits of the result in the A register can never be one. As bits 3-0
of the immediate data value were ones, however, all bits of the low-
order bed digit "fall-through" to the result. If the data at DIGITS
was 37H, the result after the AND instruction was executed would
be 37H A OFH = 07H.
The OR instruction is used to merge data into a field or to uncon-
ditionally set certain bits within a data byte. If one bed digit was in
the A register in the form 0000JJJJ2 and the second was in the B
register in the form KKKKO0002, a merged result of the form
KKKKJJJJ2 could be obtained by:
OR B MERGE TWO BCD DIGITS
As another example of the ORing function, suppose that the high-
est order, or most significant bit, in a table of ten bytes was to be
unconditionally set. The following code would set the msb of each
of the ten bytes without affecting the remainder of the byte. Note
that an ADD of 80H would not necessarily do the same thing as
adding 80H to values of 80H to FFH would reset the msb.
LD IX,TABLE SET UP INDEX
LD 13,10 SET UP COUNT
LOOP LD A,(IX) GET BYTE FROM TABLE
OR 80H SET MSB
LD (IX),A STORE BYTE BACK IN TABLE
INC IX POINT TO NEXT BYTE
DCR B DECREMENT COUNT
JP NZ,LOOP JUMP IF NOT DONE
DONE
The exclusive on instruction is not used as frequently as the AND
and OR instructions. One use is to "toggle" a bit between a one and
a zero, either for timing or for maintaining a count of two. The fol-
lowing instructions allow a loop starting at LOOP to be reentered
twice only:
166
Need help?
Do you have a question about the Z80 and is the answer not in the manual?
Questions and answers