Reputation: 24592
In x86 assembly, is esp
the stack pointer while ebp
is the heap pointer? And why do we have two data structures instead of just one?
And what do esi
/edi
represent?
Upvotes: 1
Views: 15639
Reputation: 95306
In a general, no registers on a machine architecture need to be dedicated for any purpose. As a practical matter, by designating a register to support commonly used functions, that register becomes special purpose for that/those functions.
On the x86, The ESP pointer is used by PUSH/CALL instructions and various interrupts/traps to store recoverable state such as return addresses. These are so attractive in supporting programming, and thus committed by the compiler and the OS vendors, that ESP is basically unusable for any other purpose. Because one can reference memory from an arbitrary index with respect to ESP, it can be used to manage both the "stack" and the current function stack frame.
However, the index offset of a variable (local to the current stack frame) with respect to ESP, varies as the program pushes and pops values onto the stack and ESP changes. A compiler can track all this so the right index offset can be used. But if you are coding assembly by hand, you realistically cannot do this reliably, and it is easier to have a base-of-stack-frame pointer; then the index offset of the variable can be a constant with respect to the the stack frame pointer. EBP was designated by Intel early on for this purpose, and there are even special index modes to support this, and special EBP-related instructions for building a display (a set of pointers to lexically enclosing stack frames, in Algol style).
But you/the compiler don't have to use EBP for this purpose. In a machine with a small number of registers (the x86), having another free register can help generating smaller/tighter code. On older CPUs, not using EBP had a different penalty: the index offsets from ESP for local variables tend to be larger than index offsets from a stack frame base, so using EBP as a free register got you a larger program by virtue of larger offsets used on ESP. This used to matter when fetching instructions was expensive. With instruction caches being as good as they are, this pretty much isn't an issue any more. Microsoft's C compiler (and I suspect GCC) have a switch to not use a frame pointer, thereby giving up some program size for faster code.
Heap variables are simply those that aren't in your stack frames. You can use any register to access them.
Some languages (e.g., our PARLANSE compiler) only use heap allocation; even stack frames are taken from the heap (and ESP is set to the top of the allocated block of space).
Upvotes: 15
Reputation: 8831
Typically, ESP points to the top of the stack and EBP points to the bottom of the stack frame, so they will both point to addresses on the stack, not on the heap. In most situations, it is not entirely necessary to keep a pointer to the bottom of the stack frame, so some compilers may simply use EBP as another general purpose register.
ESI and EDI are general purpose registers, with one exception: the string instructions (e.g. movs, lods) treat them as the source and destination addresses, respectively.
Upvotes: 7
Reputation: 711
The entire memory is organized into 4 segments. Code, Data, Stack and Extra. The heap is for multipurpose storage (like dynamic allocation (malloc etc)) while stack is used specifically for instruction pointer and for storing your variables when you call another procedure (like during recursion). The data segment is for global variables and static variables.
For ESI/EDI Refer this question on SO: Purpose of ESI & EDI registers?
EDIT: see this too: heap vs data segment vs stack allocation
Upvotes: 1
Reputation: 62048
While there are some things and behaviors specific to the registers xBP
, xSI
and xDI
, they aren't bound to any one function. You can use them however you like in assembly code.
Upvotes: 5