Right now I am trying to learn the assembly language on x86 systems. Therefore I am readying the book "Programming from the Ground Up". (Available for free at http://download.savannah.gnu.org/releases/pgubook/)
On page 53, the way the computer's stack works is explained:
The computer’s stack lives at the very top addresses of memory. You can push values onto the top of the stack through an instruction called pushl. [...] Well, we say it’s the top, but the "top" of the stack is actually the bottom of the stack’s memory. [...] In memory the stack starts at the top of memory and grows downward due to architectural considerations. Therefore, when we refer to the "top of the stack" remember it’s at the bottom of the stack’s memory.
That part I get. Let's say the stack's memory starts at address 0 and ends at address 11 (inclusively). That means there are currently three words (4 bytes a piece) on the stack. According to my understanding, the word that is on "top" of the stack currently occupies the addresses 8, 9, 10, and 11. (Since one word has 4 bytes and therefore occupies four storage locations in main memory). However, the book now says the following:
The stack register, %esp, always contains a pointer to the current top of the stack.
Okay, in my example the %esp register would hold the address 8. It points to the word that is currently on top of the stack. But...
Every time we push something onto the stack with pushl, %esp gets subtracted by 4 so that it points to the new top of the stack (remember, each word is four bytes long, and the stack grows downward).
What? Isn't it exactly the other way around? If I push another 4-byte-sized machine word onto the stack, this word will occupy the main memory addresses 12 to 15. Like they said: The stack grows downward. Now the %esp register points to the word that is currently on top of the stack. It starts at address 12. Before we pushed another word onto the stack, the address that was stored in %esp was 8. So %esp has clearly been added 4, not subtracted. Where do they get the subtraction from? What did I miss? I am very confused...
Help is very appreciated ;)