“What is it” came on the stack, “what is it” came back: the abstract foundation of reality

Perhaps in some future computing will forget about zeros and ones, and processors will operate not bytes and words, but so far everything is represented by the binary world and lives by its rules. What is a stack and where is it used - most likely, it will become completely unclear. Even at the beginning of its appearance, the first processor had registers, and the logic of its operation needed memory. As a result of the implementation of simple ideas, the “what is it” stack has become de facto “so necessary”.

Functions, Processor, Binary Logic

The processor manages the memory and implements the logic of cyclic code execution. A program is one or a set of functions that are a sequence of operations: choose something, do something, put it there. The operating system is also a more responsible code: it is tasked with executing and controlling other programs.

Actually a function (an independent element of the 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 accepted to develop an algorithm in the form of a sequence of separate independent elements. Each performs its own (usually only one) function - it is convenient for debugging, clearly, practical. Such programming allows us to simplify the development process, create libraries of algorithms and use them in other projects.

Transfer and return control

The program calls the function and, passing the data to it, expects to get the result. So the execution of another piece of code is initiated from the outside, so the control should be returned to the next operation after the call. This mechanism ensures the order of code execution and the achievement of the result of the program as a whole.

The code is loaded into memory and executed sequentially. To call a function (another piece of code), the stack is used. What is it? 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 get everyone below, you need to take and work out everything that is above it. A pointer always points to the current position on the stack.

Stack what is it

Initially, the return address is hit on the stack - the address of the operation following the call. Then all the data necessary for the called function is placed there. The called function selects the data, processes it, forms the result, and returns control over the last element of the stack. The command: "stack what is it" moves the pointer to the previous given, and the selected "this" goes into processing.

Registers AX, BX, CX, DX, EX have become a kind of symbols of the era of the beginning of the information era, 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 for numerous calls to various sections of code, including recursion: a call by a function of itself. There are natural limitations: memory sizes for the stack and for the code should be sufficient. If the program is correct, then its execution will be stable, and the transfer / return of control will be accurate.

What is a stack and where does it apply

The stack is filled, as the plates are stacked, which goes 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 “stack what is it” command gives a pre-agreed data between those who called and those whom he called. Upon completion of the process, it is important to return control to the address following 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 a matter of the function, how it will deal with the received data - its concern, to which table it will send its stack of data - its concern, the main thing is that the control always returns to the next operation after the call. A function can call itself only if it allows recursion, and recursion is possible only if the data is pushed onto the stack.

Fundamental abstractions

When the mundane and practical becomes the basis of the virtual perspective, one can reasonably rely on the objectivity and long life of the latter. A stack of plates gave life to the stack, and the stack ensured the operation of processors, operating systems, programs, functions, and the high-tech industry as a whole.

Apply stack in programming examples

A classic case where the simple has become the basis, but a rare case when 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 to this day, only here the ideas about it have changed. Confidently passing through the hell of strict typing, today they say: something can be anything and occupy anything in real memory, since this is absolutely not important anymore.

Assignment operators, conditions, loops, blocks ... have been transformed much less meaningfully, but the syntax has not mattered for a long time. The struggle of programming languages ​​for ideals did not give a chance for victory to one of them. The tool (compiler / interpreter / shell) must simply work, be stable and fully functional, and how and what to write / use is the concern of the programmer, not the author of the language.

The stack has gained new meaning in this global process of developing representations and technologies. Variables in the form in which it is convenient for them are superimposed on each other, and algorithms and operators are included in each other. Often you can hear about the protocol stack when one provides the other.

Stack, functionality and recursion are not synonyms at all, but they perfectly complement each other, allowing you to create unique options for presenting and processing information.

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


All Articles