delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/18/14:16:19

From: Shawn Hargreaves <Shawn AT talula DOT demon DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Asm Question
Date: Wed, 17 Sep 1997 19:41:12 +0100
Organization: None
Distribution: world
Message-ID: <CX3zROAIRCI0Ewoi@talula.demon.co.uk>
References: <Pine DOT LNX DOT 3 DOT 91 DOT 970916120927 DOT 905A-100000 AT aditya DOT unigoa DOT ernet DOT in>
NNTP-Posting-Host: talula.demon.co.uk
MIME-Version: 1.0
Lines: 46
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019