Professor Zhou Ligong's years of dedicated work on the books "Programming and Data Structure" and "Programming for AMetal Framework and Interface (I)" have sparked a learning trend in the electronics industry. With Professor Zhou's authorization, this public account has serialized the content of "Programming and Data Structure" and is committed to sharing it with readers.
The second chapter focuses on programming techniques, and this article revisits section 2.3: stacks and functions.
When a function is executed, how does it return to the calling point? Since a function may be called multiple times, and each call could originate from a different location, the called function cannot know where it should return. Therefore, the caller must inform the function of the return address when it is called.
> > >
### 2.3.1 Stack
To manage variables (data), computers typically allocate a large amount of memory. For easier management, the memory used by all variables is referred to as a stack, while the unallocated memory area is called a heap. The heap is managed by the operating system, and programmers can request blocks of memory as needed. Once allocated, that memory is reserved for the original code that requested it, and it is accessed via pointers. Because memory is a limited resource, it should be released once it is no longer needed. Failing to do so can lead to memory leaks, which cause programs to slow down or even crash.
Stacks and heaps are two common data structures used for dynamic data storage. When a program runs, the stack stores the execution context of the program. For example, local variables like argc and argv in the main() function reside on the stack, while memory dynamically allocated using malloc() is stored in the heap. These areas often share the same memory space, with the stack typically occupying the lower part and the heap the upper part. When a function is called, its stack frame is pushed onto the stack, and when it returns, the frame is popped off. Although stack memory isn't explicitly cleared, it may eventually be overwritten by new stack frames. Heap memory, on the other hand, grows downward, and over time, it becomes fragmented due to frequent allocations and deallocations.
The "stack" commonly referenced is the one supported directly by hardware. In computer science, the stack is an abstract concept that follows the LIFO (Last In, First Out) principle. Only the top element can be accessed or removed, and elements are pushed onto or popped from the stack accordingly.

Figure 2.7 shows four types of stacks: full-decrement, empty-decrement, full-increment, and empty-increment. These represent different physical implementations of the stack. "Decrement" refers to the stack pointer decreasing when data is pushed, meaning the stack grows downward, like a stalactite. "Increment" means the stack pointer increases, so the stack grows upward, like a stalagmite. "Full" indicates that the SP points to the last pushed data, while "empty" means it points to the next available slot. All four types correspond to the same logical structure, and the book will use the full-increment stack as the default unless otherwise stated.
> > >
### 2.3.2 Pushing and Popping
Assume that the data being pushed and popped is of type int. If the data is smaller than sizeof(int), it must be converted into int before being pushed, and the same conversion is applied when popping. For larger data, it needs to be split into chunks and pushed in multiple steps. Similarly, when popping, the data is retrieved in parts and reassembled.
**Push Operation**
If sp is treated as an (int *) variable, then for a full-increment stack, the push operation in C works as shown in Figure 2.8:


Figure 2.8 Schematic diagram of the stack operation
If the data length exceeds sizeof(int), it must be split into multiple pushes. The order of pushing can be low first or high first. An example of pushing in low-first order is shown in Listing 2.27.
Listing 2.27 Example of low order followed by high order stacking

It is assumed here that the data can be manipulated like an integer, and that sizeof(data) is four times larger than sizeof(int).
**Pop Operation**
If sp is treated as an (int *) variable, then for a full-increment stack, the pop operation in C works as follows (assuming the stacked data is saved to the variable data, see Figure 2.9):


Figure 2.9 Schematic diagram of the pop operation
If the data length exceeds sizeof(int), it must be reassembled after being popped in multiple steps. The order of reassembly should be the reverse of the pushing order. If the data was pushed in low-first order, it should be popped in high-first order, as shown in Listing 2.28.
Listing 2.28 Example of high order followed by low order popping

It is assumed here that the data can be bit-operated like an integer, and that sizeof(data) is four times larger than sizeof(int).
> > >
### 2.3.3 Calling and Returning Functions
Before discussing the ADT stack, let’s look at the system stack used to handle function calls at runtime. Whenever a function is called, the system creates a structure known as an activation record or stack frame, which is placed on top of the system stack. Initially, the activation record contains a pointer to the previous activation record and a return address. The pointer points to the activation record of the calling function, and the return address holds the address of the next instruction to execute after the function returns.
If the function calls other functions, its local variables (excluding static ones) and parameters are added to its activation record. A new activation record is then created for the called function and pushed onto the top of the stack. When the called function finishes, its activation record is removed. At this point, the activation record of the calling function is back on top of the stack, and the function resumes execution.
In C, the return address is stored on the hardware stack, and the called function pops it into the program counter (PC) to return to the calling point. The sample code is shown in Listing 2.29.
Listing 2.29 Call and return examples of functions

For program listing 2.29(10), the following is described in C:

For program listing 2.29(5), the following is described in C:

As seen, when a function is called, the address of the next instruction in the main function is saved on the stack. When the function returns, the program retrieves this address from the stack and continues execution from that point. In cases where functions call other functions, each return address is pushed onto the stack, allowing the program to correctly return to the correct location after each function completes.
Rental Led Video Wall
Rental Led Video Wall,Lightweight LED Message Display,Flexible Curve LED Video Wall,waterproof Rental LED video billboard,Stable Stage Video Screen Backdrop,Stage rental LED screen panel
Shenzhen Xinfei Century Technology Co., Ltd. , https://www.rgbdancing.com