| 項目 | AT&T (GAS 默認) | Intel (NASM) |
|---|---|---|
| 寄存器前綴 | %eax | eax |
| 立即數前綴 | $1 | 1 |
| 操作數順序 | src, dst | dst, src |
| 操作數長度後綴 | movl/movw/movb | dword ptr / word ptr / byte ptr |
| 間接跳轉 | jmp *%eax | jmp eax |
| 內存寻址 | section:disp(base,index,scale) | section:[base+index*scale+disp] |
.data
msg: .string "Hello, world!\n"
len = . - msg
.text
.global _start
_start:
movl $len, %edx
movl $msg, %ecx
movl $1, %ebx
movl $4, %eax
int $0x80
movl $0, %ebx
movl $1, %eax
int $0x80
section .data
msg db "Hello, world!", 0xA
len equ $ - msg
section .text
global _start
_start:
mov edx, len
mov ecx, msg
mov ebx, 1
mov eax, 4
int 0x80
mov ebx, 0
mov eax, 1
int 0x80
as --gstabs -o hello.o hello.s (含符號表)nasm -f elf hello.asmld -o hello hello.o (勿加 -s 保留符號)/usr/include/asm/unistd.h 定義 SYS_write 等棧頂依次為 argc、argv[]、envp[],popl %ecx 取 argc,循環 popl %ecx 遍歷 argv。
__asm__ __volatile__(
"movl %1, %%eax\n\t"
"movl %%eax, %0"
: "=r" (output) // 輸出約束
: "r" (input) // 輸入約束
: "%eax" // 破壞寄存器列表
);
約束修飾符:= 輸出、+ 輸入輸出、& 早期剪切。常用約束字母:r 任意寄存器、q abcd、a/b/c/d 指定 eax/ebx/ecx/edx、m 內存、I 常數。
-O2 -march=native,僅對熱點內聯匯編__volatile__ 防編譯器重排/刪除.cfi_* 指令保證異常展棧正確perf record -g 定位熱點再決定是否手寫