1. The Stack

The last few pages have covered procedures, functions and subroutines. But how are these handled in practice? How does control jump from the main program into a subroutine? How do arguments or parameters pass into and out of subroutines?

This is the role of a data structure called the 'stack'.

To form a stack, some memory is set aside especially for it by the currently running program. The stack is often a 'dynamic' structure in that it can grow and shrink according to how much data is contains.

Advantage of a stack: The dynamic nature of the stack makes it a very efficient structure because it only uses as much memory as it needs and no more.

In order to locate the last item in the stack, a special pointer is used, called the 'stack pointer'.

The stack pointer

The currently running software maintains the stack pointer. As items are POPed off the stack, the stack pointer is decremented. As items are PUSHed on to the stack, the stack pointer is incremented.

Because the stack pointer is such a basic requirement in computing, the CPU normally contains a dedicated stack pointer register for the programmer to use.


Challenge see if you can find out one extra fact on this topic that we haven't already told you

Click on this link: The stack




Copyright © www.teach-ict.com