So I have been learning assembly and came to the topic of stack, storing local, static and global variable and stuff.
But I’m having a hard time imagining it in my head.
Bottom of memory but top of stack, :S whaa??
The thing that got me confused is, every time something gets push(ed) into stack, stack pointer gets subtracted. Shouldn’t it add to it.
I mean I get the code, but its hard not still knowing what’s really happening.
It is true that on many CPU architectures the stack pointer is decreased when something is pushed onto the stack. This is really an implementation detail of the CPU, but if you find that confusing you may try visualize the stack like it is done on this diagram:
(source: eventhelix.com)
The memory addresses increase as you move down, but when you want to push something on top of the stack you place it on top of the diagram (at a lower address).
(The diagram can be found at EventHelix.com.)