- introduction to macros
- use the MPLABX IDE
- microcomputer with MPLABX IDE installed
Note: to view memory in MPLABX, look under Window/PIC
In the program test.asm used in the Introduction
to the Development Environment (part 1) lab, the following two line
code sequence was used to initialize the variable num1.
movlw b'00001111' ;store initial value
movwf num1 ; in num1
The first line moves the literal data value to the working register and
then the second line moves the value from the working register to the file register num1
The two lines are required because there is no "movlf" instruction
to move a literal directly to a file register. A similar two line code sequence
was used to initialize the variable num2
Most assemblers include support for macros. [Note that this is a function of
the assembler not the assembly language.] Macros allow a shortcut to be used
for recurring sets of code lines. To use a macro, you have to define the macro
and then invoke the macro. A macro is defined with the following syntax:
macroname MACRO arg1,arg2,...,argn
... recurring code
written in terms of args ...
are keywords that define the start and
end of a macro, respectively. macroname
in combination with the arguments arg1,arg2,...,argn
used to invoke the macro. Make sure that the variables used for the arguments
cannot be misinterpreted for other portions of the code. For example, mov
not be a good choice for an argument name.
For example, for test.asm
, you could construct the following macro.
;;; macro definitions
movlf macro literal,dest
This macro definition would typically be located between the variables
and vectors sections. Typically, assemblers require the macro definition to appear
before the first use of the macro to simplify parsing.
The two line code sequence used to initialize the variable num1
be replaced by
movlf b'00001111',num1 ;initialize num1
When the assembler encounters this line, it does not recognize "movlf" as
a valid assembly language instruction and therefore looks for a macro called "movlf".
It matches the arguments in the order it sees them and replaces every occurrence
and every occurrence of dest
It then replaces the macro invocation with the appropriate code sequence; this
is called a "macro expansion".
- Create a new project containing test.asm with the corrected CONFIG statements.
- Modify test.asm to include the movlf macro.
- Change the initialization of num1 and num2 to use the
newly defined macro.
- Build the project and look at the listing file; note the macro expansion.
You see the macro expansion because of the X=ON directive on the
list line. I strongly recommend that you leave this on so that you can verify
that the macro is expanding the way you expect.
- Open the program memory view with the symbolic option.
- Single step through the program (position the program view and source windows
so that they can be viewed simultaneously).
Demonstration - explain and demonstrate your macro.
Why would mov not be a good choice for an argument for the above code
Now put your macro into a separate file, and use an INCLUDE directive in
your program to include it. Test and see that it works.
What is the advantage to this with large programs?
Demonstration - explain and demonstrate with the new
© 2002-2009 Nora Znotinas, Wilfrid Laurier University