Stack (computing)

A stack is a data structure that works on the principle of Last In First Out (LIFO). This means that the last item put on the stack is the first item that can be taken off, like a physical stack of plates. A stack-based computer system is one that is based on the use of stacks, rather than being register based.

Missing image
Stack_demo.png
Graphic representation of a stack

The two main operations applicable to a stack are:

  • push: an item is put on top of the stack, increasing the stack size by one. As stack size is usually limited, this may provoke a stack overflow if the maximum size is exceeded.
  • pop: the top item is taken from the stack, decreasing stack size by one. In the case where there was no top item (i.e. the stack was empty), a stack underflow occurs.

Some environments that rely heavily on stacks may provide additional operations, for example:

  • dup: the top item is popped and pushed again twice, so that an additional copy of the former top item is now on top, with the original below it.
  • peek: check the topmost item.
  • swap or exchange: the two topmost items are exchanged.
  • rotate: the three topmost items exchange places in a rotary fashion. Two variants of this operation are possible, most often called left and right rotate.

Stacks are either visualised growing from the bottom up (like real-world stacks, see image, or, with the top of the stack in a fixed position, a coin holder [1] (http://www.containerstore.com/browse/Product.jhtml?CATID=77006&PRODID=64041)) or growing from left to right, so that "topmost" becomes "rightmost". This means that a right rotate will move the first element to the third position, the second to the first and the third to the second. Here are two equivalent visualisations of this process:

apple                                     banana
banana                 ==right rotate==>  cucumber
cucumber                                  apple
cucumber banana apple  ==right rotate==>  apple cucumber banana

A stack may be represented in computers inside block of memory cells, with the bottom at a fixed location, and a variable stack pointer to the current top cell. Pushing first increases the top pointer by one, pointing it to the next cell, and then fills that with the new top value. Popping first takes the top value, and then decreases the top pointer by one. Increasing and decreasing may be exchanged to yield a stack representation that grows from high addresses to lower ones.

Many CPUs have registers that can be used as stack pointers. Some, like the x86, have special instructions that implicitly use a register dedicated to the job of being a stack pointer. Others, like the PDP-11 and the 68000 family have addressing modes that make it possible to use any of a set of registers as a stack pointer. The 80x87 series of numeric coprocessors has a set of registers that can be accessed either as a stack, or as a series of numbered registers. Some microcontrollers, like the PIC and AVR, have a fixed-depth stack that is not directly accessible.

There are also a few processors which implement a stack directly in hardware. Examples include the MuP21 processor from Computer Cowboys, and the Harris RTX line.

In application programs written in a high level language, a stack can be implemented efficiently using either arrays or linked lists. In LISP there is no need to implement the stack, as the functions push and pop are available for any list.

Applications

Calculators employing reverse Polish notation use a stack structure to hold values.

A number of computer languages are stack-oriented, meaning they define most basic operations (adding two numbers, printing a character) as taking their arguments from the stack, and placing any return values back on the stack. For example, PostScript has a return stack and an operand stack, and also has a graphics state stack and a dictionary stack.

The Forth programming language uses two stacks, one for argument passing and one for subroutine return addresses. The use of a return stack is extremely commonplace, but the somewhat unusual use of an argument stack for a human-readable programming language is the reason Forth is referred to as a stack-based language.

Many virtual machines are also stack-oriented: p-Code machine, Java virtual machine.

Almost all computer environments use a special stack (the "function stack") to hold information about procedure/function nesting.

See also

External links

cs:Z醩obn韐 da:Stak de:Stack fi:Pino fr:Pile he:מחסנית (מבנה נתונים) ja:スタック ko:스택 (자료구조) nl:stack pl:Stos (informatyka) uk:Стек ru:Стек sl:Sklad_(ra膷unalni拧tvo) sv:Stack (datastruktur) zh:堆栈

Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools