Assembly Basics
x64 Registers
| 64-bit | 32-bit | 16-bit | Purpose |
|---|---|---|---|
| RAX | EAX | AX | Accumulator / return value |
| RBX | EBX | BX | Base register (callee-saved) |
| RCX | ECX | CX | Counter / 1st argument |
| RDX | EDX | DX | Data / 2nd argument |
| RSI | ESI | SI | Source index / 3rd arg |
| RDI | EDI | DI | Dest index / 4th arg (Linux) |
| RSP | ESP | SP | Stack pointer |
| RBP | EBP | BP | Base/frame pointer |
| R8-R15 | R8D-R15D | R8W-R15W | Additional (x64 only) |
Common Instructions
; Data movement
mov rax, 42 ; load immediate
mov rbx, rax ; register to register
mov [rsp], rax ; store to memory
push rbx ; push to stack
pop rbx ; pop from stack
; Arithmetic
add rax, rbx ; rax = rax + rbx
sub rax, 10 ; rax = rax - 10
imul rax, 3 ; rax = rax * 3
idiv rcx ; rax = rax / rcx, rdx = remainder
inc rax ; rax++
dec rax ; rax--
; Logical
and rax, rbx ; bitwise AND
or rax, rbx ; bitwise OR
xor rax, rax ; zero rax
not rax ; bitwise NOT
shl rax, 2 ; left shift
shr rax, 1 ; right shift
Hello World (Linux x64, NASM)
section .data
msg db "Hello, World!", 10
len equ $ - msg
section .text
global _start
_start:
mov rax, 1 ; sys_write
mov rdi, 1 ; stdout
mov rsi, msg ; buffer
mov rdx, len ; length
syscall
mov rax, 60 ; sys_exit
xor rdi, rdi ; exit code 0
syscall