Sections
.data: Stores initialized data (constants, static variables).bss: Stores uninitialized data or variables that should be zeroed out at runtime.text: Contains the executable code of the program.rodata: Stores read-only data such as string literals
In 64-bit architecture
.dataand.bssserve the same purposes
Defining data
db: Defines a byte (8bits) of datadw: Defines a word (2 bytes) of datadd: Defines a double word (4 bytes) of datadq: Defines a quad word (8 bytes) of datadt: Defines a ten-byte value typically used for extended precision floating-point numbers
Reserving space
resb: Reserves a cenrtain number of bytes in memory (typicaly used with buffers)resw: Reserves space for a number of words (2 bytes each)resd: Reserves space for a number of double words (4 bytes each)resq: Reserves space for a number of quad words (8 bytes each)
Constants
equ is used to define constants
CONSTANT_NAME equ value_or_expression
Registers
eax,ebx,ecx,edx(general purpose registers)esp(stack pointer)ebp(base pointer)esi,edi(index registers)eip(instruction pointer)
Instructions
mov(move data)add,sub(atirthmetic operations)push,pop(stack operations)cmp,jmp,je,jne(comparison and jumps)
Including
Data
; data.asm
section .data
my_data db "Hello World", 0
; main.asm
section .data
include "data.asm"
Procedures
; utils.asm
section .text
global my_proc ; make the proc exportable to be available to other files
; main.asm
extern my_proc ; now i can use this procedure
Syscalls
eax: Contains the syscall number
ebx, ecx, edx, esi, edi: These registers hold arguments ti the syscall
eax: On return this register holds the result of the syscall (0 success / <0 error)
Numbers
sys_exit (exit program): Number: 1 Arguments: Exit code (in EBX).
sys_fork (create a child process): Number: 2 Arguments: None.
sys_read (read from a file descriptor): Number: 3 Arguments: File descriptor (in EBX) Buffer (in ECX) Number of bytes to read (in EDX).
sys_write (write to a file descriptor): Number: 4 Arguments: File descriptor (in EBX) Buffer (in ECX) Number of bytes to write (in EDX).
sys_open (open a file): Number: 5 Arguments: File path (in EBX) Flags (in ECX) Mode (in EDX).
sys_close (close a file descriptor): Number: 6 Arguments: File descriptor (in EBX).
Macros
Syntax:
%macros MACRO_NAME <n> ; number of arguments
; code
; %1, %2, ... , %n arguments
%endmacro
Invokation:
MACRO_NAME arg1, arg2, ... , argn