This is a mirror of official site: http://jasper-net.blogspot.com/

Stack architecture demystified

| Friday, March 4, 2011
Eli Bendersky resolves the confusion over stack architecture with the help of some diagrams. He focuses on the Intel x86 but it equally applicable to other architectures.
Where the top of the stack is on x86

I’ve noticed more than once that some programmers are confused about the direction in which the stack grows on x86, and what "top of the stack" and "bottom of the stack" mean. It appears that this confusion is caused by a basic mismatch in the way people are used to thinking about stacks, and in the way the stack on x86 actually behaves. It doesn’t help that some online resources mistakenly call the top of the stack "bottom". The version presented here is the correct one of x86, since it relies on terminology defined in Intel’s x86 architecture manuals.
In this article, I intend to resolve this confusion with a few helpful diagrams.
The stack analogy

Back to the basics. The stack analogy is sometimes demonstrated to new students of computing with a stack of plates. You push a plate onto the stack and pop a plate off the stack. The top of the stack is where your next plate goes when pushing, and from where you take a plate when popping.

Hardware stacks

In computers, the stack is usually a specially treated region of memory. In the abstract sense, the analogy applies – you push data by placing it on the top of the stack, and pop data by taking it from the top of the stack. Note that this doesn’t address the issue of where the top of the stack is located in memory.
The stack in x86

Herein lies the source of the confusion. Intel’s x86 architecture places its stack "head down". It starts at some address and grows down to a lower address. Here’s how it looks:

stack1.png

Read more: I Programmer

Posted via email from Jasper-net

0 comments: