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

Domenico De Felice

Let's imagine I define a local array of ints with a default value of 0 in my function:

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

My understanding of this is that:

the array will be stored in the stack, by pushing 256 zeroes to the stack and consequently increasing the stack pointer. If there was no default value for the array, increasing the stack pointer would have been enough.

Now this is the assembly code produced by the 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 this may be a silly question and I am aware that each compiler may act differently, but I'm wondering where the allocation of the array with 256 zeros is happening. Were my assumptions correct or this is happening differently?

(I've not been writing assembly for quite a long time and I'm having some difficulties understanding what's going on)

Kenney

The allocation is happening here:

    subl    $1024, %esp                 

It is a sub on the stack pointer esp, because a stack grows down.

The array is cleared here:

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

What this does is:

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

And finally, here the array is released:

    addl    $1024, %esp


These are the highlights, but there are a few more instructions of note, so here's the complete listing of the (non-optimized) code:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Does Windows XP automatically initialize arrays to zero?

From Dev

How does initialization of local variables (large arrays) affect stack size?

From Dev

How are arrays initialised (to zero) in C by the compiler?

From Dev

Stack=4 in Java bytecode. How does the Java Compiler compute the 4 value? (depth of the the stack)

From Dev

Does default constructor zero-initialize member array variable?

From Dev

How to correctly Initialize the value of an array to zero?

From Dev

Does std::array default-initialize or value-initialize?

From Dev

How to identify that compiler variable value is not a default value?

From Dev

Angular: how to initialize textarea with default value

From Dev

How to initialize a generic array with a default value in Scala

From Dev

How to insist a C compiler put local variables on the stack, not in registers

From Dev

How to default time picker display value to zero?

From Dev

How does gcc push local variables on to the stack?

From Dev

How to force compiler to set a non-zero value to uninitialized variables?

From Dev

Does boost::make_shared<T[]> value initialize or default initialize the array?

From Dev

Does boost::make_shared<T[]> value initialize or default initialize the array?

From Dev

how to generate code to initialize a std::vector with a custom Zero value if it exists as T::Zero?

From Dev

how to generate code to initialize a std::vector with a custom Zero value if it exists as T::Zero?

From Dev

How to declare and initialize an associative array with string as keys and arrays as value

From Dev

How to initialize NumPy array with different default value for each column?

From Dev

How to initialize NumPy structured array with different default value for each column?

From Dev

How does alignment work with pointers to zero-sized arrays?

From Dev

How does this read a 'zero' when comparing two arrays?

From Dev

How does the java compiler assign index's in the local variables table?

From Dev

How to initialize a struct of integers to zero?

From Dev

How to initialize to zero/NULL in a template

From Dev

Initialize variable with default value in python

From Dev

MIPS - How does MIPS allocate memory for arrays in the stack?

From Dev

MIPS - How does MIPS allocate memory for arrays in the stack?

Related Related

  1. 1

    Does Windows XP automatically initialize arrays to zero?

  2. 2

    How does initialization of local variables (large arrays) affect stack size?

  3. 3

    How are arrays initialised (to zero) in C by the compiler?

  4. 4

    Stack=4 in Java bytecode. How does the Java Compiler compute the 4 value? (depth of the the stack)

  5. 5

    Does default constructor zero-initialize member array variable?

  6. 6

    How to correctly Initialize the value of an array to zero?

  7. 7

    Does std::array default-initialize or value-initialize?

  8. 8

    How to identify that compiler variable value is not a default value?

  9. 9

    Angular: how to initialize textarea with default value

  10. 10

    How to initialize a generic array with a default value in Scala

  11. 11

    How to insist a C compiler put local variables on the stack, not in registers

  12. 12

    How to default time picker display value to zero?

  13. 13

    How does gcc push local variables on to the stack?

  14. 14

    How to force compiler to set a non-zero value to uninitialized variables?

  15. 15

    Does boost::make_shared<T[]> value initialize or default initialize the array?

  16. 16

    Does boost::make_shared<T[]> value initialize or default initialize the array?

  17. 17

    how to generate code to initialize a std::vector with a custom Zero value if it exists as T::Zero?

  18. 18

    how to generate code to initialize a std::vector with a custom Zero value if it exists as T::Zero?

  19. 19

    How to declare and initialize an associative array with string as keys and arrays as value

  20. 20

    How to initialize NumPy array with different default value for each column?

  21. 21

    How to initialize NumPy structured array with different default value for each column?

  22. 22

    How does alignment work with pointers to zero-sized arrays?

  23. 23

    How does this read a 'zero' when comparing two arrays?

  24. 24

    How does the java compiler assign index's in the local variables table?

  25. 25

    How to initialize a struct of integers to zero?

  26. 26

    How to initialize to zero/NULL in a template

  27. 27

    Initialize variable with default value in python

  28. 28

    MIPS - How does MIPS allocate memory for arrays in the stack?

  29. 29

    MIPS - How does MIPS allocate memory for arrays in the stack?

HotTag

Archive