Behold the Stack, Part III

Whether you simulate a stack in your C code or just ignore the concept altogether, your program constantly uses the processor’s stack. Specifically, if the code contains a function, it uses this stack not only to call and return from the function, but to pass and return arguments as well.

All programming languages use the stack for function calls and returns. You can see this process in action when you use a debugger to run your code. The Code::Blocks debugger is excellent for this purpose.

In Code::Blocks, create a new C language project. Ensure that a Debug version is created. Then write this sample code:

#include <stdio.h>

void f(void)
{
    puts("Function f()");
}

int main()
{
    puts("Main function");
    f();

    return(0);
}

Build the code and fix any typoes, if necessary.

To debut the code, click the cursor on Line 9. On the Debug toolbar, click the Run to Cursor button. Refer to Figure 1 for visual assistance.

Figure 1. Checking the esp (stack pointer) register in the Code::Blocks debugger.

Figure 1. Checking the esp (stack pointer) register in the Code::Blocks debugger.

The program runs, but halts at the instruction at Line 9. At this point, you can open a register dump to see what’s going on with the processor: Click the Debugging Windows button (refer to Figure 1) and choose CPU Registers. The window appears as shown in the figure.

Examine the esp register, the stack pointer, on your screen. In Figure 1, it shows address 0x60ff00.

Click the Step Into button until the yellow triangle is inside the f() function, as shown in Figure 2.

Figure 2. The effect of calling a C language function on the processor's esp stack pointer register.

Figure 2. The effect of calling a C language function on the processor’s esp stack pointer register.

Observe the esp (stack pointer) register. Its value on my PC changes to 0x60fee0. What’s happened is that the value of the eip (instruction pointer) register has been pushed to the stack. Other values may be pushed as well, but the effect is that the stack has changed.

Click the Step Into button until the function call returns. At that point, you see the esp register restored to its original value.

When you pass arguments to a function, the values are also pushed to the stack. To retrieve the values, internally the function pops them from the stack and then does whatever.

When a value is returned from a function, it’s pushed to the stack as well, then recovered after the return. The overhead for the function call is handled by the compiler, so you don’t have to think about the specific stack operations in your C code. Yet, the stack is used internally to call and return from a function.

Beyond the processor’s stack pointer register, you can simulate stack operations in any programming language. The simulated stack operates like the processor’s stack pointer, but it’s not the same thing as manipulating the stack pointer directly.

In next week’s Lesson, I introduce a program that simulates stack storage.

Leave a Reply