understand gcc assembly output

0x00 generate assembly code when compile

gcc -S -o test.s test.c

sample code

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>

int main(int argc, char* argv[])
{
int i = 0;
for (i=0; i<10; i++) {
printf("hello123");
}
return 0;
}

assembly code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
	.file	"test.c"
.section .rodata
.LC0:
.string "hello123"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $32, %rsp
movl %edi, -20(%rbp)
movq %rsi, -32(%rbp)
movl $0, -4(%rbp)
movl $0, -4(%rbp)
jmp .L2
.L3:
movl $.LC0, %edi
movl $0, %eax
call printf
addl $1, -4(%rbp)
.L2:
cmpl $9, -4(%rbp)
jle .L3
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4"
.section .note.GNU-stack,"",@progbits

0x01 basic acknowledge of assembly [AT&T]

  • Register
  • Move

0x02 Reference