Assembly Language - Macros
A macro is a code-reuse mechanism provided by NASM that lets you expand code templates at assembly time, reducing the need to repeatedly write similar code.
What is a macro
A macro is a compile-time text substitution mechanism, expanded by the assembler's preprocessor before assembly.
Unlike a procedure, macros have no CALL/RET overhead -- each time a macro is used, the assembler directly copies the macro's content to the point of use.
A macro is not a function call and has no stack-frame overhead; but the executable becomes larger because each expansion copies a fresh set of code.
Single-line macros: %define
%define is a single-line macro with simple syntax:
Example
; Single-line macro example
; Define a constant
%define
MAX_SIZE
256
%define
APP_NAME
'runoob'
; Define a macro with parameters (macro function)
%define
mul_by_2
(
x
)
(
x
2
)
%define
sum3
(
a
,
b
,
c
)
(
(
a
)
(
b
)
(
c
)
)
section
.text
global
_start
_start
:
mov
eax
,
MAX_SIZE
; eax = 256
mov
eax
,
mul_by_2
(
10
)
; eax = (10 * 2) = 20
mov
eax
,
sum3
(
1
,
2
,
3
)
; eax = ((1)+(2)+(3)) = 6
; Redefine (%define can be changed)
%define
MAX_SIZE
512
mov
eax
,
MAX_SIZE
; eax = 512
; Undefine
%undef
MAX_SIZE
; mov eax, MAX_SIZE ; Error: MAX_SIZE is undefined
mov
eax
,
1
mov
ebx
,
0
int
0x80
%defineMacro expansion is pure text replacement. For examplemul_by_2(2+3)expands to(2+3 * 2), and due to operator precedence, the result is 8, not 10. This is why macro parameters must always be enclosed in parentheses.
Multi-line macros: %macro / %endmacro
%macro Used to define complex macros containing multiple instructions:
Example
; File path: macro_multi.asm
; Multi-line macro example
; Macro definition: exit the program
; Parameter argc: how many arguments the macro accepts
%macro
exit_program
1
mov
eax
,
1
; sys_exit
mov
ebx
,
%1
; The 1st argument is used as the exit code
int
0x80
%endmacro
; Macro definition: print a string
; Accepts 2 arguments: string address, length
%macro
print_string
2
push
eax
push
ebx
push
ecx
push
edx
mov
eax
,
4
; sys_write
mov
ebx
,
1
; stdout
mov
ecx
,
%1
; String address
mov
edx
,
%2
; String length
int
0x80
pop
edx
pop
ecx
pop
ebx
pop
eax
%endmacro
section
.data
msg
db
'Hello, RUNOOB!'
,
0xA
msg_len
equ
$
msg
section
.text
global
_start
_start
:
print_string msg
,
msg_len
; Use the macro to print a message
print_string msg
,
msg_len
; Call it again
exit_program
0
; Exit the program
Advanced usage of macro parameters
NASM macros support default parameters, argument counting, and conditional expansion:
Example
; Advanced macro parameter example
; Macro with default parameters (argument range 2-3)
%macro
debug_print
2
3
1
; At least 2 arguments, at most 3, with the 3rd defaulting to = 1
%if
%3
=
1
; If the 3rd argument = 1 (debug mode on)
push
eax
push
ebx
push
ecx
push
edx
mov
eax
,
4
mov
ebx
,
1
mov
ecx
,
%1
mov
edx
,
%2
int
0x80
pop
edx
pop
ecx
pop
ebx
pop
eax
%endif
%endmacro
; Macro with a variable number of arguments
%macro
push_registers
1
-*
; From 1 to any number of arguments
%
rep
%0
; %0 is the number of arguments
push
%1
; Expand the 1st one
%rotate
1
; Rotate the argument list to the left
%endrep
%endmacro
section
.text
global
_start
_start
:
; Use push_registers to save multiple registers
push_registers
eax
,
ebx
,
ecx
,
edx
; Expands to:
; push eax
; push ebx
; push ecx
; push edx
; Pop them correspondingly
pop
edx
pop
ecx
pop
ebx
pop
eax
mov
eax
,
1
mov
ebx
,
0
int
0x80
Local labels in macros
In macros, use %%label to define local labels, avoiding label conflicts when expanded multiple times:
Example
; Local labels in macros
; Compare two values and set the minimum
%macro
min_val
2
mov
eax
,
%1
mov
ebx
,
%2
cmp
eax
,
ebx
jle
%%
skip
; ★ Local label: each expansion generates a unique name
mov
eax
,
ebx
%%
skip
:
%endmacro
; Without local labels, expanding twice would produce duplicate skip labels
section
.text
global
_start
_start
:
min_val
10
,
5
; eax = 5
min_val
eax
,
3
; eax = 3
mov
eax
,
1
mov
ebx
,
0
int
0x80
Expanding a normal label twice in a macro causes a duplicate-label error. A local label
%%labelmakes NASM generate a unique label name on each expansion (such as..@0001.skip), avoiding conflicts.
Comparison of macros and procedures
| Feature | Macro | Procedure |
|---|---|---|
| Implementation | Compile-time text expansion | Runtime CALL/RET |
| Execution overhead | No call overhead (inlined directly) | Has CALL/RET overhead (a few clock cycles) |
| Code size | Each expansion increases the size | One copy of the code, called many times |
| Parameter type | Arbitrary text (registers, immediates, memory) | Runtime values |
| Debugging | Hard (no trace after expansion) | Easy (there is a function call stack) |
| Applicable scenarios | Short, frequently called code with flexible types | Complex logic, large functions |
Conditional assembly: %if / %elif / %else / %endif
The macro preprocessor supports conditional assembly, choosing different code to generate based on symbol definitions:
Example
; File path: cond_compile.asm
; Conditional assembly example: debug and release modes
%define
DEBUG
1
; 1=debug mode, 0=release mode
section
.data
msg
db
'Program running...'
,
0xA
msg_len
equ
$
msg
debug_msg
db
'[DEBUG] Entering function'
,
0xA
debug_len
equ
$
debug_msg
section
.text
global
_start
_start
:
%if
DEBUG =
1
; Debug mode: output debug information
mov
eax
,
4
mov
ebx
,
1
mov
ecx
,
debug_msg
mov
edx
,
debug_len
int
0x80
%endif
; Normal business logic
mov
eax
,
4
mov
ebx
,
1
mov
ecx
,
msg
mov
edx
,
msg_len
int
0x80
mov
eax
,
1
mov
ebx
,
0
int
0x80
Common scenarios for conditional assembly:
| Scenario | Example |
|---|---|
| Switching between debug and release | %if DEBUG / %else / %endif |
| Platform adaptation | %ifdef LINUX / %ifdef WINDOWS |
| Feature toggles | Control features by whether a symbol exists |
%rep repeat blocks
%rep Used to generate repeated code or data:
Example
; %rep repeat block example
section
.data
; Generate a 256-byte lookup table
; 0, 1, 4, 9, 16, 25, ... (table of squares)
%assign
i
0
; Here %rep cannot be used to generate a square table for complex calculations
; Simple example: generate the ASCII table for 0-9
digits
:
db
'0'
,
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
'8'
,
'9'
section
.text
global
_start
_start
:
; %rep repeats instructions in the code
mov
eax
,
0
%
rep
5
; Repeat 5 times
inc
eax
; eax increases by 1 each time
%endrep
; eax = 5
mov
ebx
,
eax
mov
eax
,
1
int
0x80
Overusing macros makes code hard to read and debug. When a macro body exceeds 10 lines, consider switching to a procedure call. Only in performance-critical hot paths does the inlining effect of short macros bring a clear benefit.