Thread: stack query
View Single Post
Old Sep 16th, 2005, 7:41 AM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
Different systems will do it different ways, but basically the compiler knows what offset each of the variables is from the stack frame.
In your example:
b is at stack frame - 3 * sizeof int
a is at stack frame - 2 * sizeof int
return address is at stack frame - 1 * sizeof int
d is at stack frame
e is at stack frame + 1 * sizeof int
f is at stack frame + 2 * sizeof int
g is at stack frame + 3 * sizeof int

So the compiler would keep the stack frame address in the frame pointer (probably the previous value would be pushed on the stack before calling the function). Then each access to a local variable becomes and access to a memory location that is the frame pointer + or - and offset.

Note that on many systems the stack grows downward, so the +'s and -'s would be the other way round.
The Dark is offline   Reply With Quote