Perhaps, in some future, computing will forget about zeros and ones, and processors will operate not by bytes and words, but so far everything is represented by the binary world and lives by its rules. What is a stack and where it is applied, - most likely, it will become absolutely not clear. Even at the beginning of its appearance, the first processor had registers, and the logic of its work needed memory. As a result of the implementation of simple ideas, the “what is this” stack has become a de facto “it should be.”
Functions, processor, binary logic
The processor manages memory and implements the logic of cyclic code execution. A program is one or a set of functions that are a sequence of operations: select something, do this, put something there. The operating system is also code, more responsible: it is tasked with executing and controlling other programs.
Actuallya function (an independent element of an algorithm) is just a sequence of binary operations. A program differs from a function in that it implements itself and coordinates other parts of the code.
It is customary to develop an algorithm as a sequence of separate independent elements. Each performs its own (usually only one) function - it is convenient for debugging, visual, practical. Such programming allows you to simplify the development process, create libraries of algorithms and use them in other projects.
Transfer and return of control
The program calls a function and, passing data to it, expects to get a result. So the execution of another section of the code is initiated from the outside, so the return of control must be carried out to the operation following the call. This mechanism ensures the order of code execution and the achievement of the result of the program as a whole.
Code is loaded into memory and executed sequentially. To call a function (another piece of code), the stack is used. What's this? Memory (and a pointer to it), which differs from the usual one in that each next element is placed above the previous one. Therefore, in order to receive anyone below, you need to take and work out everything that is above it. The pointer always points to the current position on the stack.
Initially, the return address, the address of the next operation after the call, gets on the stack. Then all the data necessary for the called function is placed there. The called function selects the data, processes it, generates the result, and returns control.by the last element of the stack. The command: "stack what is this" moves the pointer to the previous data, and the selected "this" is processed.
The registers AX, BX, CX, DX, EX have become a kind of symbols of the era of the beginning of the information age, but SP and SS are its main talisman. How is the stack used in programming? Examples of the past will not become architectural monuments of the beginning of the era. The stack came into the binary world along with the words "function" and "recursion" and disappeared into the general logic of the development of programming practice.
Serve data to decide what to do
The stack mechanism allows multiple calls to different parts of the code, including recursion: a function calling itself. There are natural limitations: the size of the memory for the stack and for the code must be sufficient. If the program is correct, then its execution will be stable, control transfer/return will be accurate.
The stack fills up as the plates are stacked and sent to serve the table. The fate of the plates, that is, their filling, is decided in the place where it is transferred. According to the logic of interaction, the command "stack what is this" gives a pre-agreed datum between the one who called and the one he called. At the end of the process, it is important to return control to the address next to the one that sent the stack to the table.
Very simple and effective logic. What is done on the table, how the decision is made is the function’s business, what it will do with the received data is its concern, to which table it will send its stack of data is itscare, the main thing is that control always returns to the operation following the call. A function can only call itself if it can be recursed, and recursion is only possible if data is pushed onto the stack.
When the ordinary and practical becomes the basis of the virtual perspective, one can reasonably count on the objectivity and long life of the latter. The stack of cymbals gave life to the stack, and the stack powered processors, operating systems, programs, features, and technology in general.
A classic case where the simple became the basis, but a rare case where it can be appreciated without empty arguments about syntax, semantics, programming mechanisms and priorities in the struggle of languages for survival.
A variable, as it was a memory cell, has remained so to this day, only ideas about it have changed. Having confidently passed through the hell of strong typing, today they argue: something can be anything and occupy anything in real memory, since this is what is completely irrelevant.
Assignment operators, conditions, loops, blocks… have changed much less in terms of meaning, but the syntax doesn't matter for a long time. The struggle of programming languages for ideals did not give one of them a chance to win. The tool (compiler/interpreter/shell) must simply work, be stable and fully functional, and how and what to write/use is the responsibility of the programmer, not the author of the language.
Stackacquired a new meaning in this global process of development of ideas and technologies. Variables in the form in which they are convenient are layered on top of each other, and algorithms and operators are included in each other. One often hears about a stack of protocols where one powers the other.
Stack, functionality and recursion are not synonyms at all, but they complement each other perfectly, allowing you to create unique ways of presenting and processing information.