Is the stack in memory actually a stack?

There is a section in memory called a stack that starts at the top and grows down to the heap. Is this stack the same as the LIFO stack? Is the heap below a FIFO?

When you do push and pop, does it change the stack in memory?

+4
source share
5 answers

stack is actually a LIFO stack.

It is created by the program itself while the program is running. The code that controls the stack is generated at compile time.

Further reading:

Hardware stacks

Call stack

X86 Addressing Modes

+4
source

Yes, the LIFO stack is used by computer architecture to store things like return addresses, local variables, etc. From Wikipedia

The x86 architecture has hardware support for the execution stack engine. Instructions, such as push, pop, call, and ret, are used with a properly configured stack for passing parameters, allocating space for local data, and saving and restoring call return points. The ret size command is very useful for making efficient (and quick) call calls in which the caller is responsible for restoring the stack space occupied by the parameters.

For example, when a function is called, the architecture pushes the return address, current register values, etc. onto the stack. When the function returns, this data exits the stack, so execution may resume at the previous location.

+3
source

This is a big pile of memory, but there is a stack pointer pointing to the top of the stack. Pressing it rises, the pop signal drops. But often you can cheat by simply changing the pointer, and in this way you can get a return value that has already been popped.

Not all architectures have a stack going in one direction. In the end, it does not matter. Some systems increase the push of the stack on push and decrease pop, other systems decrease when pressed and pop increase.

Example: the stack pointer is 0x100, and this increases the system. Then you click and the stack pointer is 0x104. You click on 0x108 again. You pop, go back to 0x104. Another system would start with 0x100, be brought to 0xfc, then reset to 0xf8 and pop up to 0xfc. If you reappear, you will return to 0x100. If you then subtract 8 from the stack pointer, then go back to 0xf8 so you can put them again. (Or, what the C compiler will do at the end of the function, just add / subtract 12 from the stack pointer, instead of inserting 3 local variables into 3 instructions.

+2
source

I'm not sure what you mean by “actually stack” (what is a “real” stack?), But this is conceptually the same thing: push decreases the “stack pointer”, and pop increases it.

+1
source

The "stack" you are talking about is the call stack of the program, so in this sense it is the stack. But there is no need for this: the actual implementation is hardware-specific, OS- and language-specific execution; - I used C compilers in which the call stack is implemented as a [double] linked list of stack frames allocated on the heap, similar to how this is done by operating systems running the IBM mainframe.

The drawback of the fixed-size hardware stack in Intel / Windows is that it makes the environment not very recursive. OTOH, it greatly increases the performance of the stack, since you do not need to use OS resources to allocate memory from the heap: it just increases the pointer.

+1
source

Source: https://habr.com/ru/post/1347291/


All Articles