Download Print this page

ZiLOG Z80 Handbook page 154

Hide thumbs Also See for Z80:

Advertisement

executed, presumably an error routine, otherwise the next instruc-
tion at NERROR is executed.
When two 8-bit operands are added or subtracted the sign, zero,
parity, and carry flags are affected. Examples for use of the zero flag
have been presented previously. The sign flag may be tested by a
conditional jump on P(ositive) or M(inus). The following example
tests for an ASCII character between 30H and 39H (decimal 0
through 9).
LD A,(CHAR) GET ASCII CHARACTER
TEST09 SUB 30H SUBTRACT 30
JP M,ERROR GO IF LESS THAN 30
SUB 10 SUBTRACT 10
JP P,ERROR GO IF 3A OR GREATER
OK ADD 10 THIS CHARACTER 30 TO 39
In this somewhat inefficient test (a compare is called for in place
of the SUB 10), the ASCII character is loaded and an immediate
30H is subtracted from the character. If the character is less than
ASCII 30H (decimal 0), the result is negative, the sign flag is set,
and a jump to ERROR is taken. If the character is greater or equal
to 3011, 10 is subtracted from the first result yielding a negative num-
ber for all valid ASCII characters (now 0 through 9) or a positive
number for all ASCII characters greater than ASCII 39H (decimal
9). A test at the JP causes a jump to the error routine if this limit
check fails. Finally, the decimal equivalent of the ASCII character
is restored by adding 10 to yield 0 - 9 for the converted character.
If an add or subtract results in an effective add of two 8-bit oper-
ands of similar signs, overflow is possible and can be tested by a
conditional branch on the P/V flag. Overflow will occur and the P/V
flag will be set if the result exceeds -128 (8011) or +127 (7FH ).
The following code tests for overflow and effects a jump to an error
routine if overflow has occurred.
NERROR
LD A,(OPND1) LOAD OPERAND 1
LD B,A INTO B
LD A,(OPND2) LOAD OPERAND 2
ADD A,B ADD OPND 2 TO OPND 1
JP PE,ERROR JUMP IF OVERFLOW
NO OVERFLOW HERE
The carry flag finds most use during double-precision or multiple-
precision operations . If the required precision is 16 bits , many opera-
tions can be implemented by the 16-bit arithmetic instructions dis-
cussed later in this section. For the general case, however, where
the precision may exceed 16 bits, the Z - 80 has addition and subtrac-
tion instructions that make use of the carry and allow operands to
163

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