Talk directly to the CPU. Registers, memory, opcodes. The language beneath all languages.
syscall interface. Registers rax, rdi, rsi, rdx. Linux sys_write and sys_exit.
section .text
global _start
_start:
; Register operations
mov rax, 42 ; immediate
mov rbx, rax ; register to register
mov [mem_var], rax ; register to memory
; LEA — Load Effective Address
lea rdi, [rax + rbx*4 + 10]
; Stack operations
push rax
push rbx
pop rcx
pop rdx
; Arithmetic
add rax, 10 ; rax += 10
sub rax, 5 ; rax -= 5
inc rax ; rax++
dec rax ; rax--
imul rax, 3 ; rax *= 3
xor rdx, rdx ; rdx = 0 (fast)
div rbx ; rax = rdx:rax / rbx
; Exit
mov rax, 60
xor rdi, rdi
syscall
section .data
mem_var dq 0
section .text
global _start
_start:
mov rax, 42
cmp rax, 42
je equal
jmp not_equal
equal:
mov rdi, 0
jmp exit
not_equal:
mov rdi, 1
exit:
mov rax, 60
syscall
; Loop: count from 0 to 9
xor rcx, rcx
loop_start:
cmp rcx, 10
jge loop_end
; do something with rcx
inc rcx
jmp loop_start
loop_end:
section .text
global _start
_start:
; String copy with REP
mov rsi, src
mov rdi, dst
mov rcx, 13
rep movsb ; copy RCX bytes from RSI to RDI
; Fill memory
mov rdi, buffer
mov rcx, 256
mov al, 0
rep stosb ; fill 256 bytes with 0
; System call to print
mov rax, 1 ; sys_write
mov rdi, 1 ; stdout
mov rsi, dst
mov rdx, 13
syscall
mov rax, 60
xor rdi, rdi
syscall
section .data
src db 'Hello, World', 0
dst times 13 db 0
buffer times 256 db 0