Chapter
S.
Macros
Example 4: Computed GOTO
Macro
This sample macro presents
an
implementation of a computed
GOTO
for the 8080 or 8085. The computed
GOTO, a common feature of many
high
level
languages, allows the program to jump to one of a number of
different
locations depending on the
value
of a variable. For example,
if
the variable
has
the value zero, the
program jumps to the first item
in
the list;
if
the variable
has
the value 3, the program jumps to the fourth
address
in
the list.
In
this example, the variable
is
placed
in
the accumulator.
The
list of addresses
is
defined
as
a series of
DW
directives starting at the symbolic address TABLE. This macro
(T
I
UMP)
also modifies itself with a nested
definition. Therefore,
only the first
call
to the T I
UMP
macro generates the calculated GOTO routine. Subse-
quent calls produce only the jump instruction
IMP
T ICODE.
TIUMP
TICODE:
TIUMP
MACRO
ADD
MVI
MOV
DAD
MOV
INX
MOV
XCHG
PCHL
MACRO
IMP
ENDM
ENDM
;IUMP
TO
A-TH
ADDR
IN
TABLE
A
;MULTIPLY
A
BY
2
D,O
;CLEAR
DREG
E,A
;GET
TABLE OFFSET
INTO
D&E
D
;ADD
OFFSET
TO
TABLE
ADDR
IN
H&L
E,M
;GET
1
ST
ADDRESS
BYTE
H
D,M
;GET
2ND
ADDRESS
BYTE
;1
UMP
TO
ADDRESS
;REDEFINE TIUMP
TO
SAVE
CODE
TICODE
;NEXT
CALL
JUMPS
TO
ABOVE
CODE
Notice that the definition of the T I
UMP~:
does not account for loading the address of the address t
<,Ie
into the
Hand
L registers; the
user
must load this address just before calling the T
JUMP
macro. The following
shows the coding for the address table
(TABLE)
and a typical
call
sequence for the T I
UMP
macro:
TABLE:
MVI
LXI
TIUMP
DW
A,2
H,TABLE
LOCO
DW
LOCl
DW
LOC2
The
call
sequence shown above causes a jump to
LOC2.
5-21