Keil Logo

Defining MPL Macros

The DEFINE MPL function creates a macro. The syntax for DEFINE function is:

%DEFINE (macro-name <[>parameter-list<]>) (macro-body)

%*DEFINE (macro-name <[>parameter-list<]>) (macro-body)

Where

% is the meta character that is used to indicate macro functions.
* specifies that the macro is expanded in literal mode.
macro-name is the name of the macro. Macro names may be up to 31 characters long and must start with A-Z, a-z, _, or ?. Subsequent characters may be A-Z, a-z, _, ?, or 0-9.
parameter-list is a list of parameters enclosed within and separated by delimiters.
macro-body is the text that is expanded to replace the macro name and parameter list. It may contain calls to other macros. If so, these macros are expanded as well. When you define a macro, those macro calls contained in the macro-body are not expanded until the macro is called.

Note

  • Once a macro is created, it may be redefined by a subsequent DEFINE function.
  • Macros may be defined with or without a formal parameter-list.
  • The parameter-list is a fairly free-form list. Exercise caution when specifying it.

Macros Without Parameters

Macros that have no parameters (or arguments) are defined as follows:

%*DEFINE (macro-name) (macro-body)

The definition requires the macro-name and the macro-body that is expanded when the macro is called. For example, the following macro definition:

%*DEFINE (my_asdf) (asdf)

when called with:

%my_asdf

expands to:

asdf

Macros With Parameters

Macros that have parameters (or arguments) are defined as follows:

%*DEFINE (macro-name <[>parameter-list<]>) (macro-body)

The parameter-list lists the formal parameters that are passed to the macro. Parameters from the parameter-list are used in the macro-body to fill in values when the macro is called. This allows you to design generic macros that produce code for many operations.

Parameters in the parameter-list are specified by unique identifiers (that you choose) which are separated by macro delimiters (which are typically parentheses and commas). The parameter-list is enclosed within parentheses and individual parameters are separated by commas. This is only a convention and not a requirement.

The only requirement of the parameter-list specification is that macro parameters are passed in the same way and with the same delimiters that are used when the macro is defined. For example, the parameter-list in the following macro definition:

%*DEFINE (BMOVE (src, dst, cnt)) (...)

is (src, dst, cnt). To call this macro, the parameters must be specified with the same delimiters. For example:

%BMOVE (1, 2, 3)

However, there is no reason that the parameters must be enclosed within parentheses or that they must be separated by commas. For example:

%*DEFINE (BMOVE src dst cnt) (...)

To call this macro, the parameters must be specified as follows:

%BMOVE 1 2 3

Parameters in the macro-body are represented with the parameter name preceded by the meta character (%src, %dst, and %cnt in the above example). The following macro definition shows a more complete example.

%*DEFINE (BMOVE (src, dst, cnt)) LOCAL lab (
        MOV     R2,#%cnt
        MOV     R1,#%src
        MOV     R0,#%dst
%lab:   MOV     A,@R1
        MOV     @R0,A
        INC     R0
        INC     R1
        DJNZ    R2,lab
)

Parameters may be used any number of times and in any order within the macro-body. If a parameter has the same name as the macro and is used in the macro-body, the parameter is expanded (instead of calling the macro).

If a macro has the same name as one of the parameters, the macro cannot be called within the macro-body because this would lead to infinite recursion.

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.