Macros

Defining a macro

A macro is a text template the assembler expands at assembly time. %macro names it and gives the parameter count, %endmacro closes it:

%macro WRITE 2 mov rax, 1 mov rdi, 1 mov rsi, %1 mov rdx, %2 syscall %endmacro WRITE msg, len

Parameters

Parameters are %1 through %n. The assembler pastes them into the template as text, so they can be registers, constants, or expressions:

%macro ADD3 3 mov rax, %1 add rax, %2 add rax, %3 %endmacro ADD3 10, 20, 30 ; rax = 60

Local labels

Labels inside a macro expand once per use, which clashes. The ..@ prefix makes a label local to one expansion, so two calls of the same macro never see each other's labels.

Next: Calling C functions

Article author: Arthur Isaev