How the program stack avoids overwriting parameters and local variables
 

Properties of the stack:

  • A stack will grow when new data are pushed on the stack

  • The new data pushed on the stack will not overwrite the old data stored inside the stack

How the program stack avoids overwriting parameters and local variables

Example: suppose that main( ) calls A( ) and then A( ) calls B( ):

  • When main( ) passes parameters to A( ) and A( ) creates its local variables using the stack, we will have created this stack frame for A( ):

    The variables inside this stack frame are exclusively used to store the parameters and local variables of function A( ).

How the program stack avoids overwriting parameters and local variables

Example: suppose that main( ) calls A( ) and then A( ) calls B( ):

  • Then if A( ) passes parameters to another function B( ) and B( ) creates its local variables using the stack, we will create another stack frame on top of A's stack frame for function B( ):

    The parameters and local variables of function B( ) will occupy diferent memory cells

How the program stack avoids overwriting parameters and local variables

Conclusion:

  • When caller function A( ) calls another function (B( )), the other function (B( )) will not overwrite the memory space reserved for A( )'s parameters and local variables !!!

DEMO:   /home/cs255001/demo/asm/8-sub/prelude.s