Tuesday, May 3, 2011

local variable storage allocation in assembly

the following snippet is often used when said to be allocate storage for local variables

addl $8,%esp  //allocate 8-byte storage

push %ebx     //store some value onto the stack

why not simply push the value onto the stack,but rather allocated some space in advance?

From stackoverflow
  • So that the allocated space is a fixed size, which is simplest for the compiler as the memory can be accessed with "DWORD PTR [ebp+(offset)]". The usual function prologue is something like:

    (Intel syntax) push ebp add ebp, 8 mov ebp, esp

    for a function with 8 bytes of local variables.

  • If you're asking why doesn't a compiler generate push instructions for local storage rather than direct manipulation of the stack pointer, it's a matter of efficiency.

    Automatic variables (in C anyway) are uninitialized, so the code sequence would be (I'm going to use subl since I'm used to stacks growing down in memory):

    C Code            Actual assembly        Your suggestion
    void x(void) {
        int a;        subl 8,%esp            push 0
        int b;                               push 0
        : : :         blah blah              blah blah
    }
    

    My answer is that it's unnecessary and inefficient in cases such as:

    C Code            Actual assembly        Your suggestion
    void x(void) {
        int a[100];   subl 400,%esp          push 0
                                             push 0
                                             push 0
                                             : : :
                                             push 0
        : : :         blah blah              blah blah
    

    What you propose may make sense for something like:

    C Code            Your suggestion
    void x(void) {
        int a = 7;    push 7
        int b = 9;    push 9
        int c[4];     subl 16,%esp
        : : :         blah blah
    }
    
    yfel : got it!thanks!!!

0 comments:

Post a Comment