Pages created and updated by Terry Sturtevant Date Posted: January 16, 2017

CP316: Microprocessor Systems and Interfacing

Introduction to Macros


  1. introduction to macros
  2. use the MPLABX IDE



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 ... ENDM

MACRO and ENDM are keywords that define the start and end of a macro, respectively. macroname in combination with the arguments arg1,arg2,...,argn are 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 would 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
        movlw    literal
        movwf    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 would 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 of literal with b'00001111' and every occurrence of dest with num1. It then replaces the macro invocation with the appropriate code sequence; this is called a "macro expansion".


  1. Create a new project containing test.asm with the corrected CONFIG statements.
  2. Modify test.asm to include the movlf macro.
  3. Change the initialization of num1 and num2 to use the newly defined macro.
  4. 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.
  5. Open the program memory view with the symbolic option.
  6. 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 example?

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 arrangement.

© 2002-2009 Nora Znotinas, Wilfrid Laurier University
Creative Commons License

Information on this site which is produced by Terry Sturtevant is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 Canada License.


To view pdf documents, you can download Adobe Acrobat Reader .
get Acrobat Reader
If you need to update a browser, you might try Firefox which is Get Firefox!
Since this page uses cascading style sheets for its layout, it will look best with a browser which supports the specifications as fully as possible.

If you are looking for an office package, with a word processor, spreadsheet, etc., you might try LibreOffice which is Get LibreOffice!

Go to the main page for the Department of Physics and Computer Science.

Valid XHTML 1.1

Valid CSS!

(Level AA)

Wilfrid Laurier University