Contents of the Execution Stack

Contents of the Stack


Exactly what does the stack look like anyway?

With the C programming language and UNIX operating system (as with all languages and operating systems) there are certian conventions that are adhered to when using the stack - though there are variations between architectures and implementations. These conventions revolve around the subroutine call and return.

Say we have two subroutines, A and B as follows:

A{
    B(1,2,3);
 }
B(X,Y,Z){
    .....
 }

This is an example of a typical calling convention:

Calling routine A does:

The Called routine (B) does:

Now that we are back in the calling routine (A), the routine:

During the execution of B, the stack would look like the following:

If you want to see this in action (and you should want to) you can compile a trivial C program with one subroutine calling another empty subroutine. Make sure the called subroutine has an argument or two, as well as a local variable or two.. No need to do anything with them. Compile with the -S option to produce assembly rather than an executable. Then look at the assembly to see what is going on! Can you figure out what is happening?

Here is a program you can try this on:

a(int x)
  {int y;}
main(){
  a(17);
  }

Call is x.c and go cc -S x.c

This will produce x.s, the assembly equivalent of x.c. If you want to see the ouput in various assembly languages, you must give the appropriate flags to the compiler to generate code for a different architecture.

1