Understanding Base Pointer and Stack Pointers: In Context with gcc Output

前端 未结 3 2385
时光说笑
时光说笑 2021-02-20 03:50

I have the following C program:

int main()
{
    int c[10] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2};
    return c[0];
}

and when compiled using the -S

3条回答
  •  野性不改
    2021-02-20 04:46

    First of all, the x86 stack grows downwards. By convention, rbp stores the original value of rsp. Therefore, the function's arguments reside at positive offsets relative to rbp, and its automatic variables reside at negative offsets. The first element of an automatic array has a lower address than all other elements, and thus is the furthest away from rbp.

    Here is a handy diagram that appears on this page:

    stack layout

    I see no reason why the compiler couldn't use a series of push instructions to initialize your array. Whether this would be a good idea, I am not sure.

提交回复
热议问题