x86asm notes
Sections
Section titled “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
Section titled “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
Section titled “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
Section titled “Constants”equ is used to define constants
CONSTANT_NAME equ value_or_expressionRegisters
Section titled “Registers”eax,ebx,ecx,edx(general purpose registers)esp(stack pointer)ebp(base pointer)esi,edi(index registers)eip(instruction pointer)
Instructions
Section titled “Instructions”mov(move data)add,sub(atirthmetic operations)push,pop(stack operations)cmp,jmp,je,jne(comparison and jumps)
Including
Section titled “Including”; data.asmsection .datamy_data db "Hello World", 0; main.asmsection .data include "data.asm"Procedures
Section titled “Procedures”; utils.asmsection .textglobal my_proc ; make the proc exportable to be available to other files; main.asmextern my_proc ; now i can use this procedureSyscalls
Section titled “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
Section titled “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
Section titled “Macros”Syntax:
%macros MACRO_NAME <n> ; number of arguments ; code ; %1, %2, ... , %n arguments%endmacroInvokation:
MACRO_NAME arg1, arg2, ... , argn