I need to use assembly insert to get the start and end addresses of the stack. As far as I understand, the SP register points to the beginning of the stack, but how do I find the end? I'm talking about i386 architecture. I realized that SP is the top of the stack, and BP is its bottom. So I can calculate the size using BP-SP?
And another question. Is the stack size static? It doesn't change after the start?
As far as I understand, the SP register points to the beginning of the stack ...
No. As Jester already wrote, SP
points to the "border" between the "used" and the "unused" part of the stack.
If SP
contains the value 0x1234, the memory addresses < 0x1234 are unused and the memory addresses >= 0x1234 are used.
I need to use assembly insert to get the start and end addresses of the stack.
Simple answer:
This is not possible.
Maybe the operating system provides such a function - depending on which OS you use.
More complex answer:
The CPU requires to know the "border" between the "used" and the "unused" part of the stack to perform stack operations (push
, pop
, call
, ret
...).
Therefore, the CPU has the stack pointer register (SP
) that contains the position of this "border".
The address of this "border" is the only information needed by the CPU; it does not need any other kind of information like the size, the start or the end of the stack to perform any kind of operation.
Because this information is not needed, the CPUs do not have any register or similar containing this information.
As a consequence, you cannot read out this information using assembly.