How does stack pointer work




















So it knows that the variable x is the third card from the top i. This method means that the address of each local variable or parameter does not need to be baked into the code.

Instead, all of these data items are addressed relative to the stack pointer. When you consider this, note two things: allocation and deallocation of locals is an extremely fast operation as it is just adding a number to or subtracting a number from the stack pointer. Also note how naturally this works with recursion. This is oversimplified for explanatory purposes.

In practice, parameters and locals may be put in registers as an optimization, and the stack pointer will generally be incremented and decremented by the word size of the machine, not one. To name a couple of things. Modern programming languages, as you well know, support the concept of subroutine calls most often called "function calls".

This means that:. How does the computer keep track of that? It maintains an ongoing record of which functions are waiting for which calls to return. This record is a stack—and since it's such an important one, we normally call it the stack.

The stack pointer is a hardware feature in CPUs—a register that's exclusively dedicated to keeping track of the top of the stack, and used by the CPU's instructions for branching into a subroutine and returning from it.

Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. I understand what a stack pointer is - but what is it used for? Ask Question. Asked 5 years, 11 months ago. Active 4 years, 10 months ago. Viewed 14k times. An explanation understandable by C programmers would be appreciated.

Improve this question. Because you can't see the top of the stack in ram like you can see the top of a stack of dishes. You do not take a dish from the bottom of a stack. You add one on top and someone else takes it from the top. You're thinking of a queue here.

Snowman Your edit seems to change the meaning of the question. Of course, I am always open to the possibility that I altered something and the original author is always free to roll back or otherwise edit the post.

Show 1 more comment. Active Oldest Votes. What purpose does this stack actually serve, as opposed to explaining its structure? Let's unpack that. Improve this answer. Eric Lippert Eric Lippert 45k 21 21 gold badges 86 86 silver badges bronze badges. Note that the quote you reference was mistakenly added in an edit by another user and has since been corrected, making this answer not quite address the question.

I'm pretty certain an explanation is supposed to increase clarity. I'm not entirely convinced that "the stack is part of the reification of continuation in a language without coroutines" comes even close to that :- — user Add a comment.

Here's some memory, each box is an address Graphics on the left, "high level" operation in the middle, C-ish pseudo code on the right Now let's pop Life without a stack pointer Let's see what happens if we don't have a stack pointer. Starting with pushing again Push a? Instead of using the Pascal string style of finding the end of an array-based collection tracking how many items are in the collection , let's try the C string style scan from the beginning to the end On top of that, we've also changed what were O 1 operations into O n operations.

TL;DR The stack pointer keeps track of the top of the stack, where all of the action occurs. Community Bot 1. From compiler point of view: all function arguments are passed by CPU registers - if there is not enough registers the arguments will be put on stack after function ends most registers should have same values as before entering it - so used registers are backed up on stack And from OS point of view: program can be interrupted any time so after we are done with system task we have to restore CPU state, so lets store everything on stack All of this works since we don't care how much items are already on stack or how many items someone else will add in future, we just need to know how much we moved the stack pointer and restore it after we are done.

I think it is more accurate to say that arguments are pushed on the stack, though often as an optimization registers are used instead on processors that have enough free registers for the task. That's a nit, but I think it matches better how the languages have evolved historically. Basile Starynkevitch Basile Starynkevitch Mason Wheeler Mason Wheeler Your assertion that structured programming without a stack would be impossible is false.

Programs compiled into continuation passing style consume no stack, but they are perfectly sensible programs. EricLippert: For values of "perfectly sensible" sufficiently nonsensical that they include standing on one's head and turning oneself inside out, perhaps. With continuation passing , it is possible to not need a call stack at all. Effectively, every call is a tail call and goto rather than return. MichaelT: I said "essentially" impossible for a reason. CPS can theoretically accomplish this, but in practice it becomes ridiculously difficult very quickly to write real-world code of any complexity in CPS, as Eric pointed out in a series of blog posts on the subject.

That's different from "implementing your own version of [the stack]". I'm not sure this is a good analogy. In reality links never get added or removed. And Stack is the one that comes under limited access data structures. A stack is stated as the container of elements where insertion and removal of the elements follow with the last-in-first-out LIFO theory. Here, the insertion of elements is done through push operation and removal of elements is done through a pop operation.

Also, the other definition in the technical aspect is, a stack is a LIFO data structure which is employed in the RAM area where one can store temporary data and addresses when the microprocessor jumps to a subroutine from its current task. A stack is also called a recursive data structure where pushing and popping happens continously.

It is the particular kind of buffer that stores the information in the order of top-down. When the new requests arrive, those will push down the previous requests. So, the latest request always is placed at the stack top position and the program will get its requests only from the top position.

With the entry of new requests, the stack pointer moves ahead to the subsequent physical memory address and the latest element is replicated in the new address location. In the same way, when an element is removed from the stack, the SP falls back to the next present item on the top position in the stack. In general, processors store SPfsac in the hardware registers and the ALU arithmetic and logic unit functions to calculate its value. Usually, push and pop are converted as micro-operations to individually add in or remove the SP and carry out load and store in the memory.

In the context of a programmer, an processor has only A. The PUSH means pushing or inserting an element into the stack. In the case of a push operation, we have to check whether there is a free space is available or not. If free space is available, we can go to the push operation, if free space is not available then error message occurs that is overflow.

The overflow is to be check in case of push operation respectively. The basic operation of push and pop is shown in the below figure. Figure a is the stack. See the figure 3 , suppose stack contains three elements a, b,c, and the stack is filled with an element. The overflow terminology is used when the stack is full and the algorithm of push operation is shown below. The POP means deleting the element at the top of the stack. In the case of pop operation, we have to check whether the stack is initially empty or not.

If the stack is initially empty then there occurs an underflow situation. Suppose the stack is empty still you want to pop the elements in the stack but there are no elements in the stack then it leads to stack underflow.

The underflow is to be check in case of pop operation respectively. In pop operation whatever the top element is present in the stack that should be popped or deleted, so no need to mention which element will be popped, by default the topmost element will be popped. The algorithm of pop operation is shown below. So whatever the elements I have taken is placed in the stack, now the stack is full.



0コメント

  • 1000 / 1000