Labels inside macros

To avoid the problem of multiple-defined labels resulting from multiple calls to a macro that has labels in its source statements, the programmer can direct the Assembler to generate unique labels on each call to a macro.

Assembler-generated labels include a string of the form _nnnnn where nnnnn is a 5-digit value. The programmer requests an assembler-generated label by specifying \@ in a label field within a macro body. Each successive label definition that specifies a \@ directive generates a successive value of _nnnnn, thereby creating a unique label on each macro call. Note that \@ may be preceded or followed by additional characters for clarity and to prevent ambiguity.

The following listing shows the definition of the clear macro:

Listing: Clear macro definition

clear:   MACRO
           LDX   #\1

           LDA   #16

\@LOOP:    CLR   0,X

           INCX

           DECA

           BNE   \@LOOP

        ENDM

This macro is called in the application, as listed in the following listing:

Listing: Calling the clear macro

         clear     temporary
         clear     data

The two macro calls of clear are expanded in the following manner, as listed in the following listing:

Listing: Macro call expansion

             clear temporary
               LDX   #temporary

               LDA    #16

_00001LOOP:    CLR   0,X

               INCX

               DECA

               BNE   _00001LOOP

             clear data

               LDX   #data

               LDA   #16

_00002LOOP:    CLR   0,X

               INCX

               DECA

               BNE   _00002LOOP