Defining a Macro

An HLI assembler macro is defined by using the define preprocessor directive.

For example, a macro could be defined to clear the R0 register.

Listing: Defining the ClearR0 macro.


/* The following macro clears R0. */
#define ClearR0 {__asm CLR R0;}

The source code invokes the ClearR0 macro in the following manner.

Listing: Invoking the ClearR0 macro.
  ClearR0;

And then the preprocessor expands the macro.

Listing: Preprocessor expansion of ClearR0.
  { __asm CLR R0 ; } ;

An HLI assembler macro can contain one or several HLI assembler instructions. As the ANSI-C preprocessor expands a macro on a single line, you cannot define an HLI assembler block in a macro. You can, however, define a list of HLI assembler instructions.

Listing: Defining two macros on the same line of source code.


/* The following macro clears R0 and R1. */
#define ClearR0and1 {__asm CLR R0; __asm CLR R1; }

The macro is invoked in the following way in the source code:

  ClearR0and1;

The preprocessor expands the macro:

{ __asm CLR R0 ; __asm CLR R1 ; } ;

You can define an HLI assembler macro on several lines using the line separator `\'.

Note: This may enhance the readability of your source file. However, the ANSI-C preprocessor still expands the macro on a single line.
Listing: Defining a macro on more than one line of source code
/* The following macro clears R0 and R1. */
#define ClearR0andR1 {__asm CLR R0; \

                      __asm CLR R1;}

The macro is invoked in the following way in the source code.

Listing: Calling the ClearR0andR1 macro


ClearR0andR1;

The preprocessor expands the macro.

Listing: Preprocessor expansion of the ClearR0andR1 macro.


{__asm CLR R0; __asm CLR R1; };