Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First, a small clarification. Stacks are not necessarily in DRAM. they are just a structure that can be formed in any memory: DRAM, caches, disk. </p> <p>To understand a stack, you should first understand what is a stack. It is like a stack of trays, the properties that make it a stack are: </p> <ul> <li>You can only access the top element of the stack</li> <li>It is Last In First Out, i.e., when you go to get a data from a stack you get the data that was stored last on the stack. </li> </ul> <p>The act of storing something in a stack is called PUSH and removing it is called a POP. Say I do the following to an empty stack: </p> <pre> PUSH A PUSH B PUSH C </pre> <p>Then the stack will contain </p> <pre> C - Top B A </pre> <p>Now if I execute a POP (notice there is no operand here), it will return C and the stack will contain</p> <pre> B -- top of stack A </pre> <p>So stack in processors is just a hardware implementation of the above algorithm. </p> <p>A register contains the address of the top of stack called <i> stack point </i> The ISA (Instruction Set Architecture) provides PUSH and POP instructions to access the stack variables as I showed above. </p> <p>This is a very useful construct. A stack is used to store local variables, basically temporary data that you want to remove at the end of a function call. It specifically helps with function calls. When a function is called, the variables of the newly called function's local variables are pushed on the stack. </p> <pre><code>foo(){ int a; int b; // both registers containing a and b are PUSHed here on the stack c = bar(); // pop the stack to get value of c print c } bar(){ int z; // local variables pushed on top of the stack z = ... return z; // pop all local variables of bar(), then push z on the stack } </code></pre> <p>I hope the above helps. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload