..

Assembler Directives / pseudo-operations

Assembler directives are instructions to the assembler. They provide guidance on how to process code, organize memory, and manage resources. They do not produce machine code that gets executed.

All the assembly code used here is X86.

There are some categories of assembler directives:

Data Definition Directives

These directives are used to define and allocate space for variables, constants, or arrays. They also initialize memory with values or reserve space for future use.

1message DB 'Hello', 0   ; Defines a byte array initialized with the string "Hello" and terminated by a null byte.
2num     DW 12345        ; Defines a 16-bit word initialized to 12345.

Storage Reservation Directives

These directives reserve memory for later use but do not initialize it with any specific value.

1buffer RESB 64  ; Reserves 64 bytes of memory.

Segment Definition Directives

These directives are used to define different segments in memory, such as code, data, or stack segments. This is important in older architectures where memory is organized in segments.

1DATA_SEG SEGMENT
2    ; Data declarations go here
3DATA_SEG ENDS

Control Directives

Control directives help control the flow of assembly, the inclusion of external files, or even conditional assembly.

1ORG 100h   ; Set the code origin to 0x100.

Macro Directives

Macros in assembly allow you to define a set of instructions that can be reused multiple times. Macro directives handle the declaration and invocation of macros.

1PRINT MACRO text
2    MOV AH, 9
3    LEA DX, text
4    INT 21h
5ENDM

Alignment Directives

These directives are used to align data or code in memory to specific boundaries (for example, aligning data to a 4-byte boundary).

1ALIGN 4  ; Aligns the next instruction or data to a 4-byte boundary.

Equates and Constants

These directives are used to define constants or symbolic names for values.

Equates (EQU)

1MAX_VALUE EQU 255     ; Defines a constant named MAX_VALUE with a value of 255.

An equate is a directive used to define a constant value. The syntax of EQU (short for equate) assigns a symbolic name to a constant. The assembler replaces all occurrences of the symbol with the corresponding value during assembly, but no memory is allocated for it.

 1BUFFER_SIZE EQU 64
 2NEWLINE     EQU 0x0A
 3
 4section .bss
 5buffer resb BUFFER_SIZE  ; Reserve 64 bytes of memory for buffer
 6
 7section .text
 8_start:
 9    mov al, NEWLINE      ; Move the newline character (0x0A) into register AL

%define Used in NASM, similar to EQU .

1%define MAX_SIZE 256

Using almost everything in an example

 1section .data           ; Data section
 2msg db 'Hello, world!', 0  ; String (symbol 'msg')
 3
 4section .bss            ; Uninitialized data section
 5buffer resb 64          ; Reserve 64 bytes (symbol 'buffer')
 6
 7section .text           ; Code section
 8global _start           ; Entry point (symbol '_start')
 9
10_start:
11    mov eax, 4          ; Syscall number for write
12    mov ebx, 1          ; File descriptor for stdout
13    mov ecx, msg        ; Address of the message
14    mov edx, 13         ; Length of the message
15    int 0x80            ; Make the system call
16
17    mov eax, 1          ; Syscall number for exit
18    xor ebx, ebx        ; Status code 0
19    int 0x80            ; Exit the program

Thanks, bye!

References: