0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C言語の構造体はただの変数の並び ― ARMとx86のアセンブリ比較

Posted at

生成されたアセンブリを見ると、構造体が「ただの変数を連続で並べたもの」であることがよく分かる。

C

test.c
struct  Kouzoutai {
  char c;
  int n;
  short s;
};

int main(){
  struct Kouzoutai k;
  k.c='A';
  k.n=1234;
  k.s=56;
  return 0;
}

ARM 32bit版のアセンブリ

それぞれの型で代入に使用している命令は以下の通り
char : strb 1バイト
int : str 4バイト
short : strh 2バイト

gcc -O0 -S test.c -o test.s
test.s
main:
	push	{r7}
	sub	sp, sp, #20
	add	r7, sp, #0
	movs	r3, #65        @ k.c='A' ASCIIで65
	strb	r3, [r7, #4]
	movw	r3, #1234      @ k.n=1234
	str	r3, [r7, #8]
	movs	r3, #56        @ k.s=56
	strh	r3, [r7, #12]
	movs	r3, #0
	mov	r0, r3
	adds	r7, r7, #20
	mov	sp, r7
	ldr	r7, [sp], #4
	bx	lr

x86 64bit版のアセンブリ

それぞれの型で代入に使用している命令は以下の通り
char : mov BYTE PTR 1バイト
int : mov DWORD PTR 4バイト
short : mov WORD PTR 2バイト

gcc -O0 -S -masm=intel test.c -o test.s
main:
	endbr64
	push	rbp
	mov	rbp, rsp
	mov	BYTE PTR -12[rbp], 65
	mov	DWORD PTR -8[rbp], 1234
	mov	WORD PTR -4[rbp], 56
	mov	eax, 0
	pop	rbp

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?