Mail Archives: djgpp/1997/09/18/14:16:19
Gurunandan R. Bhat writes:
>In what precise way does %ebp "characterise" a stack frame? Does it point
>to the first address on the current stack frame? If so shouldn't each
>nested procedure call redefine %ebp to set up a new stack frame for its
>use?
That's exactly what they do. At the start of each function, %ebp is set
to the top of the stack region being used within the function, and all
the parameters and local variables are accessed relative to this. The
main advantage to this is that the return address will always be at a
fixed offset from %ebp, and the version of %ebp from the previous
function will be pushed directly after this, so it is easy for debuggers
and programs like symify to walk backwards up the callstack.
>I have looked at some .s files produced by gcc and all they do is push and
>pop %ebp at each procedure call.
Not true! Try compiling a really simple C function, eg:
int x, y;
void do_something()
{
x += y;
}
With the command "gcc test.c -S -m486", you get the output:
_do_something:
pushl %ebp // store old value of %ebp
movl %esp,%ebp // load new stack frame
movl _y,%eax // do the code
addl %eax,_x
L1:
movl %ebp,%esp // reset stack pointer
popl %ebp // reset stack frame
ret
A lot of that isn't needed for such a simple function (nothing was
pushed onto the stack, so there is no need to restore %esp), but it
would be needed with a more complex real world routine...
--
Shawn Hargreaves - shawn AT talula DOT demon DOT co DOT uk - http://www.talula.demon.co.uk/
Beauty is a French phonetic corruption of a short cloth neck ornament.
- Raw text -