Thursday, 11 October 2007

Defining and using Macros keil + assembler

Defining Standard Macros


/* Macros */
Mihir macro one, two

push acc /*save the registers ( if you are using it)*/

mov a, #one

mov P1,#two

pop acc

endm /*end of Macro*/

calling From your program




Standard macros are defined as follows:

macro-name MACRO « parameter-list »
« LOCAL local-labels »


macro-name is the name of the macro.
parameter-list is an optional list of formal parameters that may be passed to the macro.
local-labels is an optional list of local labels within the macro.
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.

For example:

MOV R0, #R0_Val

defines a macro named LOAD_R0 which loads register 0 with the value of the first parameter passed to the macro.


  • Unlike MPL macros, once a standard macro is defined, it may not be redefined.
  • Macros may be defined with or without a formal parameter-list.
  • Standard macro definitions may be nested up to 9 levels deep.
  • Standard macro may be called recursively up to 9 levels deep.

Macro Parameters

Parameters in the body of the macro are represented with the parameter name (src, dst, and cnt in the above example). They 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. The macro is not called.

Standard macros may have up to 16 parameters. Parameters must be separated by commas (',') in the macro definition and invocation. For example:

mymacro MACRO   P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,P15,P16

You would invoke this macro as follows:

mymacro A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P

You may pass a NULL value for a parameter by omitting that parameter from the macro invocation. The separating comma is still required. For example:

mymacro A,,C,,E,,G,,I,,K,,M,,O,

Null parameters may be tested using the NUL macro operator.

Ref : keil Documentation

No comments:

Post a Comment