157/317
6 - STMicroelectronics Programming Tools
The following code, in the same module, invokes the macro by inserting its name in the oper-
ation field.
ld A, d1
ld d2, A
MyCode
ld A, d3
The result is, when assembled:
35
0000 R C60000
ld
A, d1
36
0003 R C70000
ld
d2, A
37
38
MyCode
38
0006
3C00
inc
CounterLo
38
0008
B601
ld
A, CounterHi
38
000A
A900
adc
A, #0
39
000C
40
000C R C60000
ld
A, d3
We see that the word
MyCode
itself does not produce any code; but the following three lines
were not present in the source text above; they have been inserted by the expansion of the
macro.
If a macro were only that, it would not be worth it. Macros can actually be complex constructs,
using replaceable parameters and conditional assembly. A well-defined macro can save lots
of lines of text and provide error-free text generation, since the expansions always conform to
the definition of the macro. This means that once a macro is fine-tuned, it can be used in sev-
eral places in the source text with a guarantee of success.
We shall study the various features that macros allow and illustrate why macros can be so
helpful.
6.1.7.1 Replaceable parameters
Macros may be defined so that they accept one or more parameters. A parameter is a char-
acter string that is passed when the macro is invoked, and that affects the result of the expan-
sion. To define a macro argument, just add the formal name of the argument after the
MACRO
pseudo-op in the macro definition. On invocation, this formal name, if used in the body of the
definition, will be replaced by the actual argument. Example: we shall build a macro that incre-
ments a data byte by two. It will perform an increment instruction twice on the byte. The defi-