c - How does the compiler initialize local arrays with a default value of zero on the stack? -


let's imagine define local array of ints default value of 0 in function:

void test() {     int array[256] = {0}; } 

my understanding of that:

the array stored in stack, pushing 256 zeroes stack , consequently increasing stack pointer. if there no default value array, increasing stack pointer have been enough.

now assembly code produced previous snippet:

test: .lfb2:     .cfi_startproc     pushl   %ebp     .cfi_def_cfa_offset 8     .cfi_offset 5, -8     movl    %esp, %ebp     .cfi_def_cfa_register 5     pushl   %edi     pushl   %ebx     subl    $1024, %esp     .cfi_offset 7, -12     .cfi_offset 3, -16     leal    -1032(%ebp), %ebx     movl    $0, %eax     movl    $256, %edx     movl    %ebx, %edi     movl    %edx, %ecx     rep stosl     addl    $1024, %esp     popl    %ebx     .cfi_restore 3     popl    %edi     .cfi_restore 7     popl    %ebp     .cfi_restore 5     .cfi_def_cfa 4, 4     ret     .cfi_endproc .lfe2:     .size   test, .-test 

i realize may silly question , aware each compiler may act differently, i'm wondering allocation of array 256 zeros happening. assumptions correct or happening differently?

(i've not been writing assembly quite long time , i'm having difficulties understanding what's going on)

the allocation happening here:

    subl    $1024, %esp                  

it sub on stack pointer esp, because stack grows down.

the array cleared here:

    movl    $0, %eax     movl    $256, %edx          movl    %ebx, %edi      movl    %edx, %ecx     rep stosl 

what is:

  • rep : repeat string operation ecx times
  • stosl: store eax in memory pointed edi , add 4 edi, or subtract 4, depending on direction flag. if it's clear (cld), edi gets incremented, , decremented otherwise. note ebx set point start of array bit earlier in code.

and finally, here array released:

    addl    $1024, %esp 


these highlights, there few more instructions of note, here's complete listing of (non-optimized) code:

pushl   %ebp                # preserve caller's ebp (decrements esp 4) movl    %esp, %ebp          # copy stack pointer ebp pushl   %edi                # preserve caller pushl   %ebx                # preserve caller subl    $1024, %esp         # allocate 1kb on stack leal    -1032(%ebp), %ebx   # esp + 1024 + 4 + 4 = ebp; equivalent mov %esp, %ebx movl    $0, %eax            # {0} movl    $256, %edx          # repeat count - have been stored in ecx directly movl    %ebx, %edi          # init edi start of array movl    %edx, %ecx          # put 256 in ecx rep stosl                   # repeat 'mov %eax, %(edi); add $4, %edi' ecx times addl    $1024, %esp         # release array popl    %ebx                # , preserved registers popl    %edi popl    %ebp                 ret 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -