macros

Using Macros in Assembly

Using Macros in Assembly

Macros in assembly are a powerful way to reuse code without manually duplicating it. They allow you to define common patterns or sequences of instructions that can be inserted wherever needed in the code.

Basic Macro Syntax

In assembly, macros are defined with the %macro directive and invoked with the % symbol:

%macro print_message 2
    mov eax, 4            ; sys_write
    mov ebx, 1            ; file descriptor (stdout)
    mov ecx, %1           ; message
    mov edx, %2           ; length
    int 0x80              ; make the system call
%endmacro
    

Example Usage of Macro

This example uses a macro to print messages:

section .data
    msg1 db 'Hello, world!', 0xA
    msg1_len equ $ - msg1

section .text
    global _start

_start:
    print_message msg1, msg1_len  ; Use the print_message macro
    ; Exit system call
    mov eax, 1            ; sys_exit
    xor ebx, ebx          ; Return code 0
    int 0x80
    

Advantages of Using Macros

Key Notes