What is the purpose of the EBP frame pointer register?

I am new to assembly language and noticed that x86 code emitted by compilers usually maintains a pointer to the framework even in release / optimization mode, when it can use the EBP register for something else. I understand why a frame pointer can make the code easier to debug and may be needed if alloca () is called inside a function. However, x86 has very few registers, and using two of them to store the location of the stack frame, when that's enough, just doesn't make sense to me. Why is lowering the frame pointer considered bad even in optimized / releases?

+78
performance assembly x86
Feb 23 '09 at 20:45
source share
5 answers

A frame pointer is a reference pointer that lets the debugger know where a local variable or argument with one constant offset is located. Although the ESP value changes at runtime, EBP remains the same, allowing you to achieve the same variable with the same offset (such as the first parameter will always be at EBP + 8, and the ESP offsets can change significantly since you will push / popping)

Why don't compilers throw out a frame pointer? Because using the frame pointer, the debugger can determine where local variables and arguments use the symbol table, since they are guaranteed to have a constant offset to EBP. Otherwise, there is no easy way to determine where a local variable is located anywhere in the code.

As Greg mentioned, it also helps to spin the stack for the debugger, since EBP provides an inverse linked list of stack frames, which allows the debugger to determine the size of the stack frame (local variables + arguments) of the function.

Most compilers provide the ability to skip pointers to frames, although this makes debugging very difficult. This option should never be used globally, even in release code. You do not know when you will need to debug a user crash.

+87
Feb 23 '09 at 20:55
source share

Just adding my two cents to the already good answers.

It is part of a good language architecture to have a stack frame chain. BP points to the current block where local subroutine variables are stored. (Locales are in negative offsets, and arguments are in positive offsets.)

The idea that it prevents the use of an excellent register in optimization poses the question: when and where is the optimization really worth it?

Optimization is only in tight cycles that 1) do not call the function, 2) where the program counter spends a significant part of its time, and 3) in the code that the compiler has ever seen (i.e. non-library functions), usually a very small part of the general code, especially on large systems.

Other code can be twisted and compressed to get rid of loops, and it just doesn't matter, because the program counter practically does not exist.

I know that you did not ask this, but in my experience, 99% of performance problems have nothing to do with compiler optimization. They have everything associated with excessive design.

+26
Feb 24 '09 at 21:43
source share

It depends on the compiler, of course. I saw optimized code emitted by x86 compilers that freely uses the EBP register as a general-purpose register. (I don’t remember which compiler I noticed with this.)

Compilers can also support EBP register maintenance to help spin the stack during exception handling, but again, it depends on the exact implementation of the compiler.

+6
Feb 23 '09 at 20:49
source share

However, x86 has very few registers

This is true only in the sense that operation codes can only address 8 registers. The processor itself will actually have many more registers than this, and use register renaming, pipelining, speculative execution, and other processor keywords to get around this limit. Wikipedia has a good introductory paragraph on what the x86 processor can do to overcome the case limit: http://en.wikipedia.org/wiki/X86#Current_implementations .

+4
Feb 23 '09 at 21:00
source share

Using stack frames has become incredibly cheap on any hardware, even remotely modern. If you have cheap freeze frames, saving multiple registers is not that important. I am sure that fast stack frames versus more registers were an engineering compromise, and won fast stop frames.

How much does it cost to keep a clean registry? Is it worth it?

+1
Feb 23 '09 at 20:51
source share



All Articles