Macros are user defined sets of instructions and directives that will be evaluated in-line with the assembler source code whenever the macro is invoked. Macros consist of sequences of assembler instructions, directives and compiler pseudo commands that can be written to accept arguments.
Their advantages are: -
· Higher levels of abstraction, improving readability and reliability.
· Consistent solutions to frequently performed functions.
· Simplified changes.
· Improved testability.
Macro’s are purely in the realms of the assembler, the compiler only builds the macro ready for the assembler to act upon.
The macro must first be defined before it can be referred to in subsequent source code. Macros are defined according to the following syntax: -
<label> MACRO [ <arg1> , <arg2> ...,
<argn> ]
:
:
ENDM
Where <label> is a valid compiler label name and <arg> is up to 20 arguments supplied to the macro (that will fit on the source line.) The values assigned to these arguments at the time the macro is invoked will be substituted wherever the argument name occurs in the body of the macro.
The body of a macro may comprise a limited set of compiler
commands, assembler directives, PICmicro assembly instructions, or assembler
macro directives. The assembler continues to process the body of the macro until an EXITM or ENDM directive is
encountered.
Limitations
Due to a problem
with the Microchip Assembler, you cannot use the #LOCAL directive to set up local labels, use an assembler
label instead.
If using BASIC
commands inside a macro, loops and conditions must be avoided. I.e. IF..THEN, WHILE…WEND, REPEAT…UNTIL,
FOR…NEXT are not allowed.
Only jump (GoTo)
or call(GoSub) an external routine,
you cannot jump to a label within your macro.
Also only use the GoTo command when the routine you are using terminates
with a return. Similarly use the Basic
GoSub Label when calling a routine from a macro.
Forward references
to macros is not permitted.
See Example Macros to see how macros
can be used in a real life situation.