Skip to content

Instantly share code, notes, and snippets.

@TakesxiSximada
Created October 11, 2016 08:48
Show Gist options
  • Save TakesxiSximada/e5adda1caffd9455f5d8ea70dc121d95 to your computer and use it in GitHub Desktop.
Save TakesxiSximada/e5adda1caffd9455f5d8ea70dc121d95 to your computer and use it in GitHub Desktop.
x86-64アセンブラのコードを確認する

x86-64アセンブラのコードを確認する

環境

$ 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オペランド という順序で表現されます。

ニーモニック

オペレーションサフィックス

ニーモニックのサフィックスに特定の文字をつけることでデータサイズを指定します。

サフィックス データサイズ 備考
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

参考

int main(){
int value;
value = 1;
++value;
++value;
++value;
return value;
}
.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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment