0

I have an assemble code of a function. I have just one local integer variable in it, but my compiler reserve space for 24 byte. Can I resize per hand the memory to be 4 byte (not 24 byte), which are minimum required for the Stack Frame?

pushl   %ebp
movl    %esp, %ebp
subl    $24, %esp
movl    $0, -12(%ebp)  //int i = 0;

P.S: I am working on 32 bit on linux

maggi
  • 1
  • What compiler are you using? – Increasingly Idiotic May 07 '18 at 17:23
  • You might try looking at something like [this](https://stackoverflow.com/questions/2554229/memory-alignment-within-gcc-structs) – Increasingly Idiotic May 07 '18 at 17:26
  • You can resize it by hand, but then the stack frame very likely won't work properly. – Robert Harvey May 07 '18 at 17:27
  • remember function parameters are on the stack as well. It could help to a.) post the C code leading to this and b.) tell the exact compiler you're using. –  May 07 '18 at 17:27
  • There's also the question of whether this code calls another function. – David Wohlferd May 07 '18 at 20:33
  • Hi, I have in my C program just one function called translate() , in which I have one local variable from integer type and which is initialized by 0. There is just this and it is the only function, which is call in main. For it the compiler save 24 Byte of memory, which I have to minimize to 4 Byte, because there is just one integer variable, that the function use – maggi May 07 '18 at 20:40
  • What is the problem exactly? You get the memory back as soon as the function returns. And the `int i = 0` line seriously looks like unoptimized code. Have you asked the compiler to generate fast and/or small code? – Bo Persson May 07 '18 at 20:49
  • 1
    If you compile with optimization, the function you describe compiles to “ret”. If you compile *without* optimization, that implies you don’t care about the generated code, so why are you asking about it. – prl May 08 '18 at 05:35
  • gcc likes to keep the stack 16-byte aligned. If you compile with optimization and `volatile int i=0;`, you'll see it uses `sub $16, %esp` regardless of alignment, which is weird. https://godbolt.org/g/KGPMgX. (ESP is 16-byte aligned before the `call` to a function, so you'd expect `sub $8, %esp` with `-fno-omit-frame-pointer` to re-align ESP, but that's not what we get.) – Peter Cordes May 10 '18 at 19:50

0 Answers0