汇编语言基础

x64 寄存器

64-bit32-bit16-bit用途
RAXEAXAX累加器/返回值
RBXEBXBX基址寄存器(被调用者保存)
RCXECXCX计数器/第1参数
RDXEDXDX数据/第2参数
RSIESISI源索引/第3参数
RDIEDIDI目标索引/第4参数
RSPESPSP栈指针
RBPEBPBP基址/帧指针
R8-R15R8D-R15DR8W-R15W额外寄存器(x64专有)

常用指令

; 数据传送 mov rax, 42 ; 加载立即数 mov rbx, rax ; 寄存器到寄存器 mov [rsp], rax ; 存入内存 push rbx ; 压栈 pop rbx ; 出栈 ; 算术 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-- ; 逻辑 and rax, rbx ; bitwise AND or rax, rbx ; bitwise OR xor rax, rax ; 清零 rax not rax ; bitwise NOT shl rax, 2 ; 左移2位 shr rax, 1 ; 右移1位

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