Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If the program counter points to the address of the next instruction to be executed, what do frame pointers do?

Tags:

c

callstack

If the program counter points to the address of the next instruction to be executed, what do frame pointers do?

like image 401
user133466 Avatar asked Dec 28 '09 00:12

user133466


Video Answer


1 Answers

It's like a more stable version of the stack pointer

Storage for some local variables and parameters are generally allocated in stack frames that are automatically freed simply by popping the stack pointer back to its original level after a function call.

However, the stack pointer is frequently being adjusted in order to push arguments on to the stack for new call levels and at least once on entry to a method in order to allocate its own local variables. There are other more obscure reasons to adjust the stack pointer.

All of this adjusting complicates the use of offsets to get to the parameters, locals, and in some languages, intermediate lexical scopes. It is perhaps not too hard for the compiler to keep track but if the program is being debugged, then a debugger (human or program) must also keep track of the changing offset.

It is simpler, if technically an unnecessary overhead, to just allocate a register to point to the current frame. On x86 this is %ebp. On entry to a function it may have a fixed relationship to the stack pointer.

Besides debugging, this simplifies exception management and may even pay for itself by eliminating or optimizing some adjustments to the stack pointer.

You mentioned the program counter, so it's worth noting that generally the frame pointer is an entirely software construct, and not something that the hardware implements except to the extent that virtually every machine can do a register + offset addressing mode. Some machines like x86 do provide some hardware support in the form of addressing modes and macro instructions for creating and restoring frames. However, sometimes it is found that the core instructions are faster and the macro ops end up deprecated.

like image 112
DigitalRoss Avatar answered Sep 21 '22 03:09

DigitalRoss