11.2: Multi-Line Macros
- Page ID
- 19926
Multi-line macros can include a varying number of lines (including one). The multi-line macros are more useful and the following sections will focus primarily on multi-line macros.
Macro Definition
Before using a multi-line macro, it must first be defined. The general format is as follows:
%macro <name> <number of arguments> ; [body of macro] %endmacro
The arguments can be referenced within the macro by %<number>, with %1 being the first argument, and %2 the second argument, and so forth.
In order to use labels, the labels within the macro must be prefixing the label name with a %%.
This will ensure that calling the same macro multiple times will use a different label each time. For example, a macro definition for the absolute value function would be as follows:
%macro abs 1 cmp %1, 0 jge %%done neg %1 %%done: %endmacro
Refer to the sample macro program for a complete example.
Using a Macro
In order to use or “invoke” a macro, it must be placed in the code segment and referred to by name with the appropriate number of arguments.
Given a data declaration as follows:
qVar dq 4
Then, to invoke the “abs” macro (twice):
mov eax, -3 abs eax abs qword [qVar]
The list file will display the code as follows (for the first invocation):
27 00000000 B8FDFFFFFF mov eax, -3 28 abs eax 29 00000005 3D00000000 <1> cmp %1, 0 30 0000000A 7D02 <1> jge %%done 31 0000000C F7D8 <1> neg %1 32 <1> %%done:
The macro will be copied from the definition into the code, with the appropriate arguments replaced in the body of the macro, each time it is used. The <1> indicates code copied from a macro definition. In both cases, the %1 argument was replaced with the given argument; eax in this example.
Macros use more memory, but do not require overhead for transfer of control (like functions).