开发者

Question about "push" and the stack

If you 开发者_高级运维push something into the stack multiple times (in a loop for example), does the stack keep growing or is the previous value replaced? For example, repeating push EDI 5 times. Would the stack have 5 EDIs?


The stack keeps growing, till some OS defined limit is reached. Then an exception is thrown. This limit is typically 256k or even megabytes on PC, to allow for recursion.

The stack doesn't keep types, so since EDI is a 32-bit register, after 5 push edi's it would contain 5 32-bit values, the same as the value in EDI


does the stack keep growing

Yes. That's why it's called a "stack". This is a very fundamental property, as it allows recursive calls (where a subroutine calls itself with different parameters).


Nothing grows. The stack pointer register is simply incremented (or decremented, depending on the CPU architecture) every time you push something, so it points to a new, but existing, memory location.

This is often referred to as the stack "growing", but it is really the same as saying in C:

int stack[100];
int sp = 0;
stack[sp++] = 42; // push

The CPU stack, like the array above, is fixed size and when it is exhausted an error of some sort (once again dependent on architecture) occurs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜