YouTip LogoYouTip

Assembly Macro

Assembly Language - Macros

A macro is a code reuse mechanism provided by NASM. It allows you to expand code templates at compile 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 compilation. Unlike procedures, macros do not have CALL/RET overheadβ€”each time a macro is used, the compiler directly copies the macro's content to the location of use. Macros are not function calls and have no stack frame overhead; however, the executable file size will be larger because a copy of the code is made with each expansion. * * *

Single-line Macros: %define

`%define` is a single-line macro with simple syntax:

Example

; Single-line macro example ; Define constants %define MAX_SIZE 256 %define APP_NAME 'tutorial' ; Define macros with parameters (macro functions) %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 undefined mov eax,1 mov ebx,0 int 0x80 > `%define` macro expansion is pure text substitution. For example, `mul_by_2(2+3)` expands to `(2+3 * 2)`, which due to operator precedence results in 8, not 10. This is why macro parameters must always be enclosed in parentheses. * * *

Multi-line Macros: %macro / %endmacro

`%macro` is used to define complex macros containing multiple instructions:

Example

; File path: macro_multi.asm ; Multi-line macro example ; Macro definition: exit program ; Parameter argc: how many parameters the macro accepts %macro exit_program 1 mov eax,1; sys_exit mov ebx,%1; 1st parameter as exit code int 0x80 %endmacro ; Macro definition: print string ; Accepts 2 parameters: 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, TUTORIAL!',0xA msg_len equ$- msg section.text global _start _start: print_string msg, msg_len ; Use macro to print message print_string msg, msg_len ; Call again exit_program 0; Exit program * * *

Advanced Usage of Macro Parameters

NASM macros support default parameters, parameter counting, and conditional expansion:

Example

; Advanced macro parameter example ; Macro with default parameters (parameter range 2-3) %macro debug_print 2-3 1; At least 2 parameters, at most 3, 3rd defaults to 1 %if%3 = 1; If 3rd parameter = 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 ; Variable number of arguments macro %macro push_registers 1-*; 1 to any number of parameters %rep%0; %0 is the number of parameters push%1; Expand the 1st %rotate 1; Rotate parameter 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 ; Correspondingly pop pop edx pop ecx pop ebx pop eax mov
← Assembly Memory MgmtAssembly Procedure β†’