arrays - Assembly version of c program on gcc version 4.6.3 -


i have written small array program in c

#include <stdio.h> int main() {     int arr[]={1,2,3,4,5};//int size 4, elements 5 size of array = 4*5 = 20     printf("%d\n", sizeof(arr));     return 0; } 

i compiled

gcc -o2 -fverbose-asm -s -c arr_n_pointer_confusion.c  

i got this,

    .section    .rodata.str1.1,"ams",@progbits,1 .lc0:     .string "%d\n"     .section    .text.startup,"ax",@progbits     .p2align 4,,15     .globl  main     .type   main, @function main: .lfb22:     .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    $16, %esp   #,     movl    $20, 8(%esp)    #,     movl    $.lc0, 4(%esp)  #,     movl    $1, (%esp)  #,     call    __printf_chk    #     xorl    %eax, %eax  #     leave     .cfi_restore 5     .cfi_def_cfa 4, 4     ret     .cfi_endproc .lfe22:     .size   main, .-main     .ident  "gcc: (ubuntu/linaro 4.6.3-1ubuntu5) 4.6.3"     .section    .note.gnu-stack,"",@progbits 

can relate steps in assembly c. why trying understand little assembly code can understand difference pointer array.

basically, because compiler knows contents of array @ compile-time, can remove array, , replace sizeof(array) 20, without having initialize array @ runtime.

.cfi_startproc pushl   %ebp    # save value of ebp (points base of stack) in stack .cfi_def_cfa_offset 8 .cfi_offset 5, -8 movl    %esp, %ebp  #, set value of base of stack top of .cfi_def_cfa_register 5 andl    $-16, %esp  # align stack 16-byte boundary, used simd instructions subl    $16, %esp   # subtract 16 value of stack pointer (reserving 16 bytes of space stack) movl    $20, 8(%esp)    # set memory 8 bytes above stack '20' movl    $.lc0, 4(%esp)  # move string "%d\n" 4 bytes above stack movl    $1, (%esp)  # set flag __printf_chk() 1, enabling stack overflow checks call    __printf_chk    # print our string xorl    %eax, %eax # 0 out eax register (i.e. store return code in eax, 0) 

. parameters passed __printf_chk (1, "%d\n", 20).


Comments