[GCC] -S 옵션?

development 2011. 11. 18. 11:58
아래와 같은 코드를 작성하였습니다.

test.c

#include <stdio.h>
int test(int i)
{
  printf("%d\n", i); return 0;
}

int main(int argc, char *argv[])
{
  int i;
  for(i=0;i<10;i++){
    test(i);
  }
  return 0;
}    
 
이 코드를 어셈블리 코드로 보고 싶다면?

gcc -S test.c    
 
-S 옵션을 붙여 주면 됩니다.
 
gcc -S test.c    
 
그럼 아래와 같이 어셈블리 코드로 볼 수 있습니다. 
 
    .file "test.c"
    .section .rodata
.LC0:
    .string "%d\n"
    .text
    .globl test
    .type test, @function
test:
.LFB0:
    .cfi_startproc
    pushl %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl %esp, %ebp
    .cfi_def_cfa_register 5
    subl $24, %esp
    movl $.LC0, %eax
    movl 8(%ebp), %edx
    movl %edx, 4(%esp)
    movl %eax, (%esp)
    call printf
    movl $0, %eax
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
.LFE0:
    .size test, .-test
    .globl main
    .type main, @function
main:
.LFB1:
    .cfi_startproc
    pushl %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl %esp, %ebp
    .cfi_def_cfa_register 5
    andl $-16, %esp
    subl $32, %esp
    movl $0, 28(%esp)
    jmp .L3
.L4:
    movl 28(%esp), %eax
    movl %eax, (%esp)
    call test
    addl $1, 28(%esp)
.L3:
    cmpl $9, 28(%esp)
    jle .L4
    movl $0, %eax
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
.LFE1:
    .size main, .-main
    .ident "GCC: (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1"
    .section .note.GNU-stack,"",@progbits
 
참고로 전 어셈코드를 못 봅니다. 능력이 안됩니다.
 
블로그 이미지

김유석0

,