The concept of a stack in assembly language is not easy to understand. In the previous article, the usage of the stack was introduced, and in this article, I will briefly discuss my understanding of the role of the stack.
In life, a stack can be likened to a temporary residence for travelers. In assembly language, the stack serves as a temporary storage area for data used by programs. I also thought of other instances in our lives where stacks are utilized. I believe these ideas are connected to the concept of a stack. This also leads me to think that the use of stacks in assembly language may have been inspired by certain scenarios in life. Alternatively, some technologies in computers may originate from everyday life.
One example of a stack in everyday life is a handbag. When we go out, we often carry items, and these items are usually stored in a handbag.
When we leave, we place items into the handbag one by one, which is equivalent to pushing onto the stack. The first items placed in are at the bottom, while the last items are on top.
When we reach our destination, we take out the items one by one, which is equivalent to popping from the stack. When retrieving items, the last ones placed in are the first to be taken out, which is known as Last In, First Out (LIFO). The first items placed in are the last to be taken out, which is known as First In, Last Out (FILO).
This handbag, like a stack, can be used to temporarily store items. The reason items are retrieved in this order is due to convenience. In programming, there is a similar principle: the data that is used last is more likely to be needed again soon.
The primary function of a handbag is to carry items, but it also serves the purpose of temporarily storing items. Both functions coexist.
Another example is the countertop of a kitchen cabinet used when washing dishes. When we wash dishes, where do we place the washed dishes? They can only be temporarily placed on the countertop. When we wash the first round of dishes, they are stacked one by one on the countertop, which is similar to pushing onto the stack. The first dishes placed are at the bottom, while the last ones are on top.
When we need to wash a second round, we start taking them from the top, which is similar to popping from the stack. The countertop serves as the stack space, used to temporarily store the dishes.
In summary, in life, the role of a stack can be used to temporarily store certain objects or items; in assembly language, the stack is used to temporarily store data, freeing up the original space occupied by data for new code to use.
Here, I provided two examples from everyday life to illustrate the use of a stack. From this, we can see that stacks are not far from us; they may be present in our surroundings, and at times, we may be using this method without realizing it. This makes me believe that life is filled with admirable wisdom that we need to summarize and learn from.