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.
- DB (Define Byte): Allocates a byte (8 bits) and can initialize it with a value.
- DW (Define Word): Allocates a word (16 bits) of memory.
- DD (Define Doubleword): Allocates a double word (32 bits).
- DQ (Define Quadword): Allocates a quadword (64 bits).
Storage Reservation Directives
These directives reserve memory for later use but do not initialize it with any specific value.
- RESB (Reserve Byte): Reserves a specified number of bytes.
- RESW (Reserve Word): Reserves a specified number of words (16 bits each).
- RESD (Reserve Doubleword): Reserves a specified number of doublewords (32 bits each).
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.
- SEGMENT: Marks the beginning of a segment.
- ENDS: Marks the end of a segment.
Control Directives
Control directives help control the flow of assembly, the inclusion of external files, or even conditional assembly.
- ORG: Specifies the origin or starting address of the code or data.
- INCLUDE: Inserts the contents of another file into the current assembly file.
- IF/ELSE/ENDIF: Allows conditional assembly based on predefined conditions, often used to assemble different code based on configuration.
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.
- MACRO: Defines the beginning of a macro.
- ENDM: Marks the end of a macro.
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).
- ALIGN: Aligns the next instruction or data to a specified 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.
- EQU: Defines a constant or a symbolic name for a value.
- %DEFINE: Similar to
#define
in C, used to define macros.
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.
- Equates are resolved during assembly, not at runtime.
- They improve readability by replacing hard-coded values with meaningful names.
- No storage space is used since they are not variables, just symbolic names for values.
%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:
- SCHILDT, Herbert. STL Programming from the Ground Up. New York: McGraw-Hill, 1999.
- PC Assembly Book
- x86 Assembly Guide