Crossware

Table of Contents        Previous topic       Next topic       

CROSS ASSEMBLER->Assembler Macros->Introduction to Assembler Macros

A macro is a powerful tool to enable you to generate multiple lines of code from a single statement.  Coupled with conditional assembly, macros can perform complex tasks.  

There are two aspects to a macro:

- the macro definition
- the macro expansion.

The macro definition does not generate any code.  It simply defines a set of statements for later use during the macro expansion phase.  When the macro is defined it is given a unique name by the user.  The statements included in the macro definition can then be regenerated (expanded) simply by including that name in the operation field of a source code statement (known as a macro call).  Moreover, arguments can be included in the operand field following the macro call.  These arguments are used during expansion to directly replace dummy arguments in the macro definition. Thus a macro can be called many times and different arguments can cause different code to be generated for each call.  The dummy arguments may be in the label, operation or operand fields of the statements in the macro definition and so a very flexible system is possible. The following example illustrates a simple macro definition and macro call. (See sections  and  for descriptions of the directives MACR and ENDM).

Firstly the source code listing:

    NAM    MACXAMPL
    TTL    Macro Definition and Call
    LIST
    SYM
    MEX
    ORG 6000H

* This is the macro definition:

macxampl MACR  
        LD \0,$60
        LD \1,$52
        ADD \0,\1
        ENDM

* This is the macro call:
    
    macxampl A,B

And now the listing generated during assembly:

   1                              NAM     MACXAMPL    
   2                              TTL     Macro Definition and
                                          Call
   3                              LIST                
   4                              SYM                 
   5                              MEX                 
   6 6000                         ORG     6000H       
   7
   8                    * This is the macro definition:
   9
  10                    macxampl MACR  
  11                             LD \0,$60
  12                             LD \1,$52
  13                             ADD \0,\1
  14                             ENDM
  15
  16                    * This is the macro call:
  17
  18                              macxampl A,B         
     6000 3E 60                   LD      A,$60       
     6002 06 52                   LD      B,$52       
     6004 80                      ADD     A,B         
  19
  20
Assembly complete
Bytes filed: 5
0 errors
0 warnings

The statements that can be included in a macro definition are:

- executable instructions
- assembler directives
- macro calls

A macro cannot be called until it is has been defined.  The macro definition must therefore precede any reference to it.