$ uname -v
Darwin Kernel Version 14.5.0: Wed Jul 29 02:26:53 PDT 2015; root:xnu-2782.40.9~1/RELEASE_X86_64
$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.0 (clang-700.0.72)
Target: x86_64-apple-darwin14.5.0
Thread model: posix
main.c::
int main(){
int value;
value = 1;
++value;
++value;
++value;
return value;
}
このコードを使ってアセンブリコードを出力します。
$ gcc -S main.c
次のコードが出力されます。
.section __TEXT,__text,regular,pure_instructions
.macosx_version_min 10, 10
.globl _main
.align 4, 0x90
_main: ## @main
.cfi_startproc
## BB#0:
pushq %rbp
Ltmp0:
.cfi_def_cfa_offset 16
Ltmp1:
.cfi_offset %rbp, -16
movq %rsp, %rbp
Ltmp2:
.cfi_def_cfa_register %rbp
movl $0, -4(%rbp)
movl $1, -8(%rbp)
movl -8(%rbp), %eax
addl $1, %eax
movl %eax, -8(%rbp)
movl -8(%rbp), %eax
addl $1, %eax
movl %eax, -8(%rbp)
movl -8(%rbp), %eax
addl $1, %eax
movl %eax, -8(%rbp)
movl -8(%rbp), %eax
popq %rbp
retq
.cfi_endproc
.subsections_via_symbols
使っているコンパイラがgccなのでAT&T構文のアセンブリコードが出力されます。
AT&T構文は概ね ニーモニック srcオペランド dstオペランド
という順序で表現されます。
- データ転送 https://en.wikibooks.org/wiki/X86_Assembly/Data_Transfer
- 処理制御 https://en.wikibooks.org/wiki/X86_Assembly/Control_Flow
- 演算 https://en.wikibooks.org/wiki/X86_Assembly/Arithmetic
- 論理演算 https://en.wikibooks.org/wiki/X86_Assembly/Logic
- シフト/ローテート https://en.wikibooks.org/wiki/X86_Assembly/Shift_and_Rotate
- その他の命令 https://en.wikibooks.org/wiki/X86_Assembly/Other_Instructions
- 割り込み https://en.wikibooks.org/wiki/X86_Assembly/X86_Interrupts
ニーモニックのサフィックスに特定の文字をつけることでデータサイズを指定します。
サフィックス | データサイズ | 備考 |
---|---|---|
b | 8bit | - |
s | 16bit | short と single float(32bit) |
w | 16bit | - |
l | 32bit | int とfloat(64bit) |
q | 64bit | quad |
t | 80bit | ten bytes 80bit floating point |
数値リテラルには $
をつけます。
movl $2 %ax
- X86 Assembly/GAS Syntax https://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax
- OpenBSD kernel hack memo annex / GNU Assembler http://caspar.hazymoon.jp/OpenBSD/annex/gas.html
- Programming Lab / x86拡張命令一覧表 http://sky.geocities.jp/krypton_pl/x86/x86ext.htm