Value to subtract from ESP register?
I have a question which regards mostly gcc. If isn't appropriate here, I apologize, maybe someone can point me to a more appropriate mailing list.
So... the problem: When gcc compiles a function, the first thing the function does is to subtract a certain value from the ESP register, to make room for local variables. This value is always >= than the size of local variables, but I'd be curious to know what algorithm gcc uses to decide how much greater the value will be.
An example: I have this code in C:
---start------------------
void f1() {
char d[3];
}void f2() {
char d[4];
}---end--------------------
The assembly output for f1 and f2 functions is (gcc -S test.c):
---start------------------
f1:
pushl %ebp
movl %esp, %ebp
subl $24, %esp leave
ret
f2:
pushl %ebp
movl %esp, %ebp
subl $4, %esp leave
ret
---end--------------------
I would like to know why the subtracted value is 24 when the local variable is 3 bytes and 4 when the variable is 4 bytes.
This is the optimazation code that gcc uses.It differs from version to version and you can discover it only if you work with different arguments(values) in your arrays.After a step the subtraction of ESP is the same e.g for 128 and so on.But i think that somewhere on the documentation of gcc is already written.
Quoted 5 lines · Chris Nanakos
This is the optimazation code that gcc uses.It differs from version to version and you can discover it only if you work with different arguments(values) in your arrays.After a step the subtraction of ESP is the same e.g for 128 and so on.But i think that somewhere on the documentation of gcc is already written.
Thanks. I've looked in the gcc's documentation but I was unable to find the answer to my problem (or maybe I didn't look in the right documentation).
Anyway, about my specific example, how subtracting 24 instead of 4 is gonna optimize anything?
Quoted 2 lines · zavandi
Anyway, about my specific example, how subtracting 24 instead of 4 is gonna optimize anything?
In most cases, gcc rounds the size up to keep the stack pointer aligned to a 16-byte boundary. If you were to generate functions for 1-byte, 2-byte, 3-byte etc arrays you would note that for arrays of 1-16 bytes (with certain exceptions; see below), it subtracts 24; for 17-32 bytes, it subtracts 40; for 33-48 bytes, it subtracts 56, etc (the pushed EIP and EBP account for the other 8 bytes). This is consistent with the fact that the i386 architecture caches data in aligned 16-byte blocks (sometimes referred to as "paragraphs").
However, it treats sizes of 1, 2, 4 and 8 bytes as special cases, allocating 4 bytes for the first three cases and 8 bytes for the last. Again, this very much looks like something which would be done for optimisation reasons.
If you need to know the specific reasons, you would probably have to ask the people who actually wrote the code. And even they might not actually know why. Optimisation tends to involve dealing with opposing pressures. Any particular technique will speed up some programs but slow down others; the optimal approach can often only be determined by trial and error. If a certain combination tends to make code run slightly faster, it may be adopted without anyone actually analysing it in detail.
Signature · 1 line
Glynn Clements <[email protected]>