CyberPsychotic wrote:

> In many manuals/references and gdb messages itself i met the term frame
> (change stack frame, no frame selected, when i try to disassemble
> sometimes etc). Anyone could clarify the term "frame" to me and how it
> applys here?

A function typically begins with code of the form:

        pushl %ebp
        movl %esp,%ebp
        subl $12,%esp

and ends with:

        movl %ebp,%esp
        popl %ebp
        ret

Upon entry to the function, the stack looks like this:

        param 3
        param 2
        param 1
        return address
esp ->

After the entry code has been executed, the stack looks like this:

        param 3
        param 2
        param 1
        return address
        saved ebp
ebp ->  local var 1
        local var 2
        local var 3
esp ->

The exit code restores esp, undoing the effect of the `subl $12,%esp'
(and any other stack adjustments which occur during the function), and
then resores ebp to the value that it had upon entry.

Here, ebp is the `frame pointer'. The code which comprises the
function refers to the function's parameters as ebp+<offset>, and
local variables as ebp-<offset>. If you compile with `-g', the
compiler adds data to the object files to associate specific offsets
with specific named parameters/variables, so that you can reference
them by name from within the debugger.

If you compile with the -fomit-frame-pointer switch, the entry code
isn't generated. Instead, parameters and local variables are
referenced relative to esp.

However, if esp changes (e.g. by pushing arguments onto the stack in
order to call a function, or calling alloca()), then the offsets
change. Whilst the compiler can keep track of which offset corresponds
to which parameter/variable, the debugger can't. Consequently, using
-fomit-frame-pointer means that you can't reference parameters or
variables by name from within the debugger.

The purpose of the -fomit-frame-pointer switch is to make an extra
register (ebp) available for use within the function. On the Intel
processors, which have very few registers compared to most other
architectures, this can provide a significant increase in performance
for certain types of code.

OTOH, some processors have a dedicated `frame pointer' register, which
can't be used for anything else. On these processors, the
-fomit-frame-pointer switch does nothing.

Incidentally, the Intel processors have dedicated `enter' and `leave'
instructions which do exactly the same thing as the entry and exit
code shown above. However they are slower than the code shown above
(on the 486 at least; I don't know the relative timings on the
Pentium).

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to