LW Tool Chain
Prev | Chapter 3. LWASM | Next

# 3.7. Macros

LWASM is a macro assembler. A macro is simply a name that stands in for a series of instructions. Once a macro is defined, it is used like any other assembler directive. Defining a macro can be considered equivalent to adding additional assembler directives.

Macros may accept parameters. These parameters are referenced within a macro by the a backslash ("\") followed by a digit 1 through 9 for the first through ninth parameters. They may also be referenced by enclosing the decimal parameter number in braces ("{num}"). The special expansion "\*" translates to the exact parameter string, including all parameters, passed to the macro. These parameter references are replaced with the verbatim text of the parameter passed to the macro. A reference to a non-existent parameter will be replaced by an empty string. Macro parameters are expanded everywhere on each source line. That means the parameter to a macro could be used as a symbol or it could even appear in a comment or could cause an entire source line to be commented out when the macro is expanded.

Parameters passed to a macro are separated by commas and the parameter list is terminated by any whitespace. This means that neither a comma nor whitespace may be included in a macro parameter.

Macro expansion is done recursively. That is, within a macro, macros are expanded. This can lead to infinite loops in macro expansion. If the assembler hangs for a long time while assembling a file that uses macros, this may be the reason.

Each macro expansion receives its own local symbol context which is not inherited by any macros called by it nor is it inherited from the context the macro was instantiated in. That means it is possible to use local symbols within macros without having them collide with symbols in other macros or outside the macro itself. However, this also means that using a local symbol as a parameter to a macro, while legal, will not do what it would seem to do as it will result in looking up the local symbol in the macro's symbol context rather than the enclosing context where it came from, likely yielding either an undefined symbol error or bizarre assembly results.

Note that there is no way to define a macro as local to a symbol context. All macros are part of the global macro namespace. However, macros have a separate namespace from symbols so it is possible to have a symbol with the same name as a macro.

Macros are defined only during the first pass. Macro expansion also only occurs during the first pass. On the second pass, the macro definition is simply ignored. Macros must be defined before they are used.

The following directives are used when defining macros.

`macroname` MACRO [NOEXPAND]

  This directive is used to being the definition of a macro called `macroname`. If `macroname` already exists, it is considered an error. Attempting to define a macro within a macro is undefined. It may work and it may not so the behaviour should not be relied upon.

  If NOEXPAND is specified, the macro will not be expanded in a program listing. Instead, all bytes emitted by all instructions within the macro will appear to be emitted on the line where the macro is invoked, starting at the address of the line of the invokation. If the macro uses ORG or other directives that define symbols or change the assembly address, these things will also be hidden (except in the symbol table) and the output bytes will appear with incorrect address attribution. Thus, NOEXPAND should only be used for macros that do not mess with the assembly address or otherwise define symbols that should be visible.

ENDM

  This directive indicates the end of the macro currently being defined. It causes the assembler to resume interpreting source lines as normal.

Prev | Home | Next
Assembler Directives | Up | Structures