user6575913 user6575913 - 7 months ago 41
C Question

Assembly local variable and parameters

I have the following code


int adunare(int a,int b)
int c=3;
int d=6;

while(c>10) c++;
if(c>15) return a+b+c+d;
else return a+b+c-d;

int main()
int w=5;
int y=6;

My question is in assembly it puts the variable w,y at the [esp+24] ,[esp+28].

Why it puts my variables there?

I know that local variables are always [ebp-....].

Why here it is not [ebp-..]?


I know that local variables are always [ebp-....]

They're not (as evidenced by your question too, I suppose).

It's legal for a compiler to compile really naively, always using a frame pointers (even in functions that don't do variable-size stack allocations) and always putting locals on the stack in the first place (which is definitely not a rule). In a first year course in university, it is sometimes pretended that that's normal, to keep things simple.

Not using a frame pointer is usually possible, it works mostly the same as if you had used one except that offsets are calculated relative to the stack pointer, which you are now only allowed to move in predictable ways. Because it has to be predictable (that is, every instructions that references a stack slot can use a constant offset to do so), it cannot be used in functions that use alloca or VLAs. In your example function neither are used, so no frame pointer is necessary.

Also in general you should not expect local variables to correspond to specific stack slots in the first place, regardless of how they are addressed. It is allowed, common, and often a good thing, to keep a variable in a register over the entire lifetime of the variable. Especially if that life time is short or if the usage-density is very high. On top of that, variables with non-overlapping life times can (and should, because it reduces the stack size) share stack slots, since it would be the case that at most one of them needs storage at any one moment (thanks to the assumption of non-overlapping life times).

It's also allowed to have a variable hop from one stack slot to an other, this might happen when you swap two variables in a way that allows the swap to be resolved "virtually", by just changing which stack slot the variables live in and not actually exchanging the data.