Sie sind auf Seite 1von 4

WHAT IS A STACK? A stack is an abstract data type frequently used in computer science.

A stack of objects has the property that the last object placed on the stack will be the first object removed. This property is commonly referred to as last in, first out queue, or a LIFO. Several operations are defined on stacks. Two of the most important are PUSH and POP. PUSH adds an element at the top of the stack. POP, in contrast, reduces the stack size by one by removing the last element at the top of the stack. WHY DO WE USE A STACK? Modern computers are designed with the need of high-level languages in mind. The most important technique for structured programs introduced by high-level languages is the procedure or function. From one point of view, a procedure call alters the flow of control just as a jump does, but unlike a jump, when finished performing its task, a function returns control to the statement or instruction following the call. This high-level abstraction is implemented with the help of the stack. The stack is also used to dynamically allocate the local variables used in functions, to pass parameters to the functions, and to return values from the function. THE STACK REGION A stack is a contiguous block of memory containing data. A register called the stack pointer (SP) points to the top of the stack. The bottom of the stack is at a fixed address. Its size is dynamically adjusted by the kernel at run time. The CPU implements instructions to PUSH onto and POP off of the stack. The stack consists of LOGICAL stack frames that are pushed when calling a function and popped when returning. A stack frame contains the parameters to a function, its local variables, and the data necessary to recover the previous stack frame, including the value of the instruction pointer at the time of the function call. Depending on the implementation the stack will either grow down (towards lower memory addresses), or up. In our examples we'll use a stack that grows down. This is the way the stack grows on many computers including the Intel, Motorola, SPARC and MIPS processors. The stack pointer (SP) is also implementation dependent. It may point to the last address on the stack, or to the next free available address after the stack. For our discussion we'll assume it points to the last address on the stack.

STACK BASED CPUs There are several CPUs that were designed without general purpose registers. Instead these CPUs had a fast memory stack that could be used instead of the registers. In order to effectively use such a system, mathematical expressions have to be re-written in a slightly different manner.

REGISTER STACK If the microprocessor has enough registers, A stack can be organized as a collection of a finite number of registers. The stack pointer register in this situation would contain the index of the register containing the item at the top of the stack. With a register stack there would also be a need for a couple of flag registers to determine when the stack is completely full or completely empty.

In a 64-word stack, the stack pointer contains 6 bits. The one-bit register FULL is set to 1 when the stack is full; EMPTY register is 1 when the stack is empty. The first item stored in the stack at address 1, the last item is stored at address 0. The data register DR holds the data to be written into or read from the stack. The following are the micro-operations associated with the stack i) Initialization SP <- 0, EMPTY <- 1, FULL <- 0

ii) Push A memory write operation inserts the word from DR into the top of the stack. SP <- SP + 1 M[SP] <- DR If (SP = 0) then (FULL <- 1) Note that SP becomes 0 after 63 EMPTY <- 0 iii) Pop DR <- M[SP] SP <- SP - 1 If (SP = 0) then (EMPTY <- 1) FULL <- 0 Note that item C has been read out but not physically removed. This does not matter because when the stack is pushed, a new item is written in its place.

A stack can exist as a standalone unit or can be implemented in a random access memory attached to a CPU. The implementation of a stack in the CPU is done by assigning a portion of a memory to a stack operation and using a processor register as a stack pointer. Following figure shows a portion of computer memory partitioned into three segments; program, data and stack. The program counters PC points as the address of the next instruction in the program. The address registers AR points at an array of data. The stack pointer SP points at the top of the stack. The three registers are connected to a common address bus, and either one can provide an address for memory. PC is used during the fetch phase to read an instruction. AR is used during the execute phase to read an operand. SP is used to push or pop items into or from the stack.