LD B, 3 FOR SHIFT SUBROUTINE
CALL MSHRL SHIFT DE 3 PLACES RIGHT
JP LOOP CONTINUE
DONE POP BC RESTORE ALL
POP AF REGISTERS AND
POP DE RETURN
RET
The preceding subroutine illustrates another important aspect of
subroutine use. It is important to know which registers the subrou-
tine uses and which registers are saved. BXOAS saves all registers
used, such as DE (shifted and masked), A (used for conversion),
and C (used for iteration counting). In addition, BXOAS calls an-
other subroutine, MSHRL, which uses the B register, so that the
B register must also be saved. The preceding sequence, by the way,
uses three levels of subroutines. BXOAS calls MSHRL, which calls
SHRL. This nesting of subroutines can be extended to as many levels
as convenient. Stack operations are automatic and create no prob-
lems as long as there is a return for every call and a pop for every
push.
All of the above examples have dealt with unconditional CALLS
and RETurns, but conditional calls and returns may also be made.
The conditions and mnemonics for these are the same as for the con-
ditional extended jumps, as shown in Table 14-2.
The uses of the conditional calls and returns are identical to the
applications of their unconditional counterparts. In the following
example, an argument is loaded into A and subroutine ABSVAL is
called if the number is negative. A bit test instruction tests the sign
bit to reset the Z flag if the number is negative.
LD A, (IX) LOAD FIRST ARGUMENT
BIT 7, A TEST SIGN
CALL NZ, ABSVAL TAKE ABS VAL IF NECESSARY
An e ample of a conditional return is in the following routine
which finds the integer portion of the square root of an 8-bit number.
Table 14-2. Conditional Calls and Returns
Flag
Call
Return
NZ
CALL NZ,SRTN
RET NZ
Z
CALL Z,SRTN
RET Z
NC
CALL NC,SRTN
RET NC
C
CALL C,SRTN
RET C
PO
CALL PO,SRTN
RET PO
PE
CALL PE,SRTN
RET PE
P
CALL P,SRTN
RET P
M
CALL M,SRTN
RET M
214
Need help?
Do you have a question about the Z80 and is the answer not in the manual?