next up previous contents index

[ENGN3213 Home]

Stacks

  You may have seen stacks in other courses. This section summarises their main features and operation.

A stack consists of a sequence of memory locations addressed by a register called the stack pointer. Items are stored in a last in first out (LIFO) scheme.

Items are stored on the stack by pushing them onto the stack. Items are retrieved in reverse order by poping them from the stack. The stack pointer points to the top of the stack, i.e. the last item pushed onto the stack. The bottom of the stack is the first item.

Stacks may grow into lower memory, or into higher memory.

Any of the address registers may be used to create stacks. However, register A7 plays a special role, indexing the system stack (one for each mode of operation). In supervisor mode, A7 is the supervisor stack pointer (SSP), while in user mode A7 is the user stack pointer (USP). System stack play a very important role in system operation, and performs functions such as storing the value of the program counter when a subroutine is called.

  Let SP denote a stack pointer. We now describe the operation of a stack growing into lower memory (as the system stacks do), Figure 105.


  
Figure 105: Stack growing into lower memory.
\begin{figure}
\begin{center}
\epsfig{file=images/stack1.eps}\end{center}\end{figure}

PUSH: $ (SP) \longleftarrow (SP)-k$
  $((SP)) \longleftarrow \ {\rm item}$
POP: $ ((SP)) \ \longrightarrow \ {\rm item \ retrieved}$
  $ (SP) \longleftarrow (SP)+k$

The value of k depends on operand size: k=1 for byte, k=2 for word, and k=4 for longword.

The top of the stack is pointed to by (SP), the address in SP. The contents of the top of stack is denoted ((SP)).

Figure 106 illustrates the stack push operation.


  
Figure 106: Stack push operation.
\begin{figure}
\begin{center}
\epsfig{file=images/stack2.eps}\end{center}\end{figure}

Initially (SP)=1000, with contents ((SP))=FFFF. The word item 0x1A39 (2 bytes, or 16 bits) is pushed onto the stack. (SP) is decremented by k=2 to get the new value (SP)=0FFE, pointing to the top of the stack. In assembly language, this push operation is achieved with, for example, the instruction

MOVE.W #$1A39,-(SP)
(This is an example of address register indirect addressing with predecrement.)


next up previous contents index

[ENGN3213 Home]

ANU Engineering - ENGN3213