Learning x86 assembly and the frame pointer is blowing my mind a little. I learned from this SO question that EBP makes life awesome for debugging. That's great, but I was curious, "What else is EBP used for?" Looking at the call stack Wikipedia article, EBP is necessary for dynamic stack allocation.
As I've always been under the assumption that dynamic allocation goes on the heap. So, why would I want to use dynamic stack allocation - why isn't the heap good enough? And, how is EBP useful for this?
%EBP - Base Pointer. This 32-bit register is used to reference all the function parameters and local variables in the current stack frame. Unlike the %esp register, the base pointer is manipulated only explicitly. This is sometimes called the "Frame Pointer".
A frame pointer (the ebp register on intel x86 architectures, rbp on 64-bit architectures) contains the base address of the function's frame. The code to access local variables within a function is generated in terms of offsets to the frame pointer.
address of the top of the stack. base pointer (EBP): register containing the. address of the bottom of the stack frame. instruction pointer (EIP): register containing. the address of the instruction to be executed.
You assume wrongly that ebp
is necessary for stack frame allocation. This is not true, esp
can be used directly.
The use of ebp
as a stack frame pointer is in no way necessary nowadays. There are a few points, where is was useful:
In 16 bit code, the use of sp
(the stack pointer) in addressing was severely limited, relative addressing wasn't directly possible at all in x86, where bp
could be used in every addressing mode available.
The reasoning for this limited support were possibly the compilers available back then, it's much simpler to generate code with frame pointers then to keep track of the ever changing sp
. There are many more processors which had special support for stack frame instructions, like leave
or enter
, but AFAIK nobody else but Intel has gone so far to cripple the real sp
in that process :-)
Debugging. It is much easier to unwind a stack when frame pointers are available, but modern debuggers can do even without.
If you want to allocate 100 byte space on the stack, just do sub esp, #100
, access the space with move [esp + x]
where x
is between 0 and 99 and clean up with add esp, #100
and you are done. I would even argue that using stack frame pointers in hand written assembly is like copying the behavior of a thirty years old compiler, when the compilers were really stupid and couldn't do without. It's in no way necessary nor useful if you are writing assembly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With