Chapter
6. Programming
Techniques
First call
to
ADSUB:
H
L
ADSUB:
D
o
06
PLiST
08
PLiST
+1
OEH
PLiST +2
The second time ADSUB
is
called, the
Hand
L registers point to the parameter list LlST2. The A and B
registers are
loaded with 10 and 35 respectively, and the sum
is
stored at LlST2+2. Return
is
then made to
the instruction at RET2.
Note
that
the parameter lists PLiST and LlST2 could appear anywhere
in
memory without altering the results
produced by ADSUB.
This approach does have its limitations, however.
As
coded, ADSUB must receive a list
of
two and only two
numbers to
be
added, and they must be contiguous
in
memory. Suppose
we
wanted a subroutine (GENAD)
which
would add
an
arbitrary number
of
bytes, located anywhere
in
memory, and leave the sum
in
the accumu-
lator.
This can be done
by
passing the subroutine a parameter list which
is
a list of addresses
of
parameters, rather
than the parameters themselves, and signifying the end
of
the parameter list
be
a number whose first byte
is
FFH (assuming that no parameters
will
be stored above address
FFOOH).
Call
to
GENAD:
H
L
GENAD:
D
D
~
l
PARMI
ADRI
PARM4
ADR2
ADR3
PARM3
ADR4
FFFF
PARM2
As
implemented below,
GE
NAD
saves the current sum (beginning with zero)
in
the C register. It then loads the
address
of
the first parameter into the D and E registers.
If
this address
is
greater than or equal
to
FFOOH,
it
reloads the accumulator with the sum
held
in
the C register and returns
to
the calling routine. Otherwise, it
6-5