<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
stack	B-Application
is	O
an	O
abstract	O
data	O
type	O
that	O
serves	O
as	O
a	O
collection	B-Application
of	O
elements	O
,	O
with	O
two	O
main	O
operations	O
:	O
</s>
<s>
Additionally	O
,	O
a	O
peek	B-Application
operation	O
can	O
,	O
without	O
modifying	O
the	O
stack	B-Application
,	O
return	O
the	O
value	O
of	O
the	O
last	O
element	O
added	O
.	O
</s>
<s>
Calling	O
this	O
structure	O
a	O
stack	B-Application
is	O
by	O
analogy	O
to	O
a	O
set	O
of	O
physical	O
items	O
stacked	O
one	O
atop	O
another	O
,	O
such	O
as	O
a	O
stack	B-Application
of	O
plates	O
.	O
</s>
<s>
The	O
order	O
in	O
which	O
an	O
element	O
added	O
to	O
or	O
removed	O
from	O
a	O
stack	B-Application
is	O
described	O
as	O
last	O
in	O
,	O
first	O
out	O
,	O
referred	O
to	O
by	O
the	O
acronym	O
LIFO	B-Application
.	O
</s>
<s>
As	O
with	O
a	O
stack	B-Application
of	O
physical	O
objects	O
,	O
this	O
structure	O
makes	O
it	O
easy	O
to	O
take	O
an	O
item	O
off	O
the	O
top	O
of	O
the	O
stack	B-Application
,	O
but	O
accessing	O
a	O
datum	O
deeper	O
in	O
the	O
stack	B-Application
may	O
require	O
taking	O
off	O
multiple	O
other	O
items	O
first	O
.	O
</s>
<s>
Considered	O
as	O
a	O
linear	O
data	B-General_Concept
structure	I-General_Concept
,	O
or	O
more	O
abstractly	O
a	O
sequential	O
collection	B-Application
,	O
the	O
push	B-Application
and	I-Application
pop	I-Application
operations	O
occur	O
only	O
at	O
one	O
end	O
of	O
the	O
structure	O
,	O
referred	O
to	O
as	O
the	O
top	O
of	O
the	O
stack	B-Application
.	O
</s>
<s>
This	O
data	B-General_Concept
structure	I-General_Concept
makes	O
it	O
possible	O
to	O
implement	O
a	O
stack	B-Application
as	O
a	O
singly	O
linked	B-Data_Structure
list	I-Data_Structure
and	O
as	O
a	O
pointer	O
to	O
the	O
top	O
element	O
.	O
</s>
<s>
A	O
stack	B-Application
may	O
be	O
implemented	O
to	O
have	O
a	O
bounded	O
capacity	O
.	O
</s>
<s>
If	O
the	O
stack	B-Application
is	O
full	O
and	O
does	O
not	O
contain	O
enough	O
space	O
to	O
accept	O
another	O
element	O
,	O
the	O
stack	B-Application
is	O
in	O
a	O
state	O
of	O
stack	B-Error_Name
overflow	I-Error_Name
.	O
</s>
<s>
A	O
stack	B-Application
is	O
needed	O
to	O
implement	O
depth-first	B-Algorithm
search	I-Algorithm
.	O
</s>
<s>
Stacks	B-Application
entered	O
the	O
computer	B-General_Concept
science	I-General_Concept
literature	O
in	O
1946	O
,	O
when	O
Alan	O
M	O
.	O
Turing	O
used	O
the	O
terms	O
"	O
bury	O
"	O
and	O
"	O
unbury	O
"	O
as	O
a	O
means	O
of	O
calling	O
and	O
returning	O
from	O
subroutines	O
.	O
</s>
<s>
Subroutines	O
had	O
already	O
been	O
implemented	O
in	O
Konrad	O
Zuse	O
's	O
Z4	B-Device
in	O
1945	O
.	O
</s>
<s>
Klaus	O
Samelson	O
and	O
Friedrich	O
L	O
.	O
Bauer	O
of	O
Technical	O
University	O
Munich	O
proposed	O
the	O
idea	O
of	O
a	O
stack	B-Application
in	O
1955	O
and	O
filed	O
a	O
patent	O
in	O
1957	O
.	O
</s>
<s>
In	O
March	O
1988	O
,	O
by	O
which	O
time	O
Samelson	O
was	O
deceased	O
,	O
Bauer	O
received	O
the	O
IEEE	O
Computer	O
Pioneer	O
Award	O
for	O
the	O
invention	O
of	O
the	O
stack	B-Application
principle	O
.	O
</s>
<s>
Stacks	B-Application
are	O
often	O
described	O
using	O
the	O
analogy	O
of	O
a	O
spring-loaded	O
stack	B-Application
of	O
plates	O
in	O
a	O
cafeteria	O
.	O
</s>
<s>
Clean	O
plates	O
are	O
placed	O
on	O
top	O
of	O
the	O
stack	B-Application
,	O
pushing	O
down	O
any	O
already	O
there	O
.	O
</s>
<s>
When	O
a	O
plate	O
is	O
removed	O
from	O
the	O
stack	B-Application
,	O
the	O
one	O
below	O
it	O
pops	O
up	O
to	O
become	O
the	O
new	O
top	O
plate	O
.	O
</s>
<s>
In	O
many	O
implementations	O
,	O
a	O
stack	B-Application
has	O
more	O
operations	O
than	O
the	O
essential	O
"	O
push	O
"	O
and	O
"	O
pop	O
"	O
operations	O
.	O
</s>
<s>
An	O
example	O
of	O
a	O
non-essential	O
operation	O
is	O
"	O
top	O
of	O
stack	B-Application
"	O
,	O
or	O
"	O
peek	B-Application
"	O
,	O
which	O
observes	O
the	O
top	O
element	O
without	O
removing	O
it	O
from	O
the	O
stack	B-Application
.	O
</s>
<s>
This	O
could	O
be	O
done	O
with	O
a	O
"	O
pop	O
"	O
followed	O
by	O
a	O
"	O
push	O
"	O
to	O
return	O
the	O
same	O
data	O
to	O
the	O
stack	B-Application
,	O
so	O
it	O
is	O
not	O
considered	O
an	O
essential	O
operation	O
.	O
</s>
<s>
If	O
the	O
stack	B-Application
is	O
empty	O
,	O
an	O
underflow	O
condition	O
will	O
occur	O
upon	O
execution	O
of	O
either	O
the	O
"	O
stack	B-Application
top	O
"	O
or	O
"	O
pop	O
"	O
operations	O
.	O
</s>
<s>
Additionally	O
,	O
many	O
implementations	O
provide	O
a	O
check	O
if	O
the	O
stack	B-Application
is	O
empty	O
and	O
one	O
that	O
returns	O
its	O
size	O
.	O
</s>
<s>
A	O
stack	B-Application
can	O
be	O
easily	O
implemented	O
either	O
through	O
an	O
array	B-Data_Structure
or	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
,	O
as	O
stacks	B-Application
are	O
just	O
special	O
cases	O
of	O
lists	O
.	O
</s>
<s>
What	O
identifies	O
the	O
data	B-General_Concept
structure	I-General_Concept
as	O
a	O
stack	B-Application
,	O
in	O
either	O
case	O
,	O
is	O
not	O
the	O
implementation	O
but	O
the	O
interface	O
:	O
the	O
user	O
is	O
only	O
allowed	O
to	O
pop	O
or	O
push	O
items	O
onto	O
the	O
array	B-Data_Structure
or	O
linked	B-Data_Structure
list	I-Data_Structure
,	O
with	O
few	O
other	O
helper	O
operations	O
.	O
</s>
<s>
The	O
following	O
will	O
demonstrate	O
both	O
implementations	O
,	O
using	O
pseudocode	B-Language
.	O
</s>
<s>
An	O
array	B-Data_Structure
can	O
be	O
used	O
to	O
implement	O
a	O
(	O
bounded	O
)	O
stack	B-Application
,	O
as	O
follows	O
.	O
</s>
<s>
The	O
first	O
element	O
,	O
usually	O
at	O
the	O
zero	O
offset	O
,	O
is	O
the	O
bottom	O
,	O
resulting	O
in	O
array[0]	O
being	O
the	O
first	O
element	O
pushed	O
onto	O
the	O
stack	B-Application
and	O
the	O
last	O
element	O
popped	O
off	O
.	O
</s>
<s>
The	O
program	O
must	O
keep	O
track	O
of	O
the	O
size	O
(	O
length	O
)	O
of	O
the	O
stack	B-Application
,	O
using	O
a	O
variable	O
top	O
that	O
records	O
the	O
number	O
of	O
items	O
pushed	O
so	O
far	O
,	O
therefore	O
pointing	O
to	O
the	O
place	O
in	O
the	O
array	B-Data_Structure
where	O
the	O
next	O
element	O
is	O
to	O
be	O
inserted	O
(	O
assuming	O
a	O
zero-based	O
index	O
convention	O
)	O
.	O
</s>
<s>
Thus	O
,	O
the	O
stack	B-Application
itself	O
can	O
be	O
effectively	O
implemented	O
as	O
a	O
three-element	O
structure	O
:	O
</s>
<s>
structure	O
stack	B-Application
:	O
</s>
<s>
procedure	O
initialize( stk	O
:	O
stack	B-Application
,	O
size	O
:	O
integer	O
)	O
:	O
</s>
<s>
procedure	O
push( stk	O
:	O
stack	B-Application
,	O
x	O
:	O
item	O
)	O
:	O
</s>
<s>
procedure	O
pop( stk	O
:	O
stack	B-Application
)	O
:	O
</s>
<s>
Using	O
a	O
dynamic	B-Data_Structure
array	I-Data_Structure
,	O
it	O
is	O
possible	O
to	O
implement	O
a	O
stack	B-Application
that	O
can	O
grow	O
or	O
shrink	O
as	O
much	O
as	O
needed	O
.	O
</s>
<s>
The	O
size	O
of	O
the	O
stack	B-Application
is	O
simply	O
the	O
size	O
of	O
the	O
dynamic	B-Data_Structure
array	I-Data_Structure
,	O
which	O
is	O
a	O
very	O
efficient	O
implementation	O
of	O
a	O
stack	B-Application
since	O
adding	O
items	O
to	O
or	O
removing	O
items	O
from	O
the	O
end	O
of	O
a	O
dynamic	B-Data_Structure
array	I-Data_Structure
requires	O
amortized	O
O(1 )	O
time	O
.	O
</s>
<s>
Another	O
option	O
for	O
implementing	O
stacks	B-Application
is	O
to	O
use	O
a	O
singly	O
linked	B-Data_Structure
list	I-Data_Structure
.	O
</s>
<s>
A	O
stack	B-Application
is	O
then	O
a	O
pointer	O
to	O
the	O
"	O
head	O
"	O
of	O
the	O
list	O
,	O
with	O
perhaps	O
a	O
counter	O
to	O
keep	O
track	O
of	O
the	O
size	O
of	O
the	O
list	O
:	O
</s>
<s>
structure	O
stack	B-Application
:	O
</s>
<s>
procedure	O
initialize( stk	O
:	O
stack	B-Application
)	O
:	O
</s>
<s>
Pushing	B-Application
and	I-Application
popping	I-Application
items	O
happens	O
at	O
the	O
head	O
of	O
the	O
list	O
;	O
overflow	O
is	O
not	O
possible	O
in	O
this	O
implementation	O
(	O
unless	O
memory	O
is	O
exhausted	O
)	O
:	O
</s>
<s>
procedure	O
push( stk	O
:	O
stack	B-Application
,	O
x	O
:	O
item	O
)	O
:	O
</s>
<s>
procedure	O
pop( stk	O
:	O
stack	B-Application
)	O
:	O
</s>
<s>
Some	O
languages	O
,	O
such	O
as	O
Perl	B-Language
,	O
LISP	B-Language
,	O
JavaScript	B-Language
and	O
Python	B-Language
,	O
make	O
the	O
stack	B-Application
operations	O
push	B-Application
and	I-Application
pop	I-Application
available	O
on	O
their	O
standard	O
list/array	O
types	O
.	O
</s>
<s>
Some	O
languages	O
,	O
notably	O
those	O
in	O
the	O
Forth	B-Application
family	O
(	O
including	O
PostScript	B-Language
)	O
,	O
are	O
designed	O
around	O
language-defined	O
stacks	B-Application
that	O
are	O
directly	O
visible	O
to	O
and	O
manipulated	O
by	O
the	O
programmer	O
.	O
</s>
<s>
The	O
following	O
is	O
an	O
example	O
of	O
manipulating	O
a	O
stack	B-Application
in	O
Common	B-Language
Lisp	I-Language
(""	O
is	O
the	O
Lisp	B-Language
interpreter	O
's	O
prompt	O
;	O
lines	O
not	O
starting	O
with	O
""	O
are	O
the	O
interpreter	O
's	O
responses	O
to	O
expressions	O
)	O
:	O
</s>
<s>
Several	O
of	O
the	O
C++	B-Language
Standard	I-Language
Library	I-Language
container	B-Application
types	O
have	O
and	O
operations	O
with	O
LIFO	B-Application
semantics	O
;	O
additionally	O
,	O
the	O
template	O
class	O
adapts	O
existing	O
containers	B-Application
to	O
provide	O
a	O
restricted	O
API	B-General_Concept
with	O
only	O
push/pop	B-Application
operations	O
.	O
</s>
<s>
Java	B-Language
's	O
library	O
contains	O
a	O
class	O
that	O
is	O
a	O
specialization	O
of	O
.	O
</s>
<s>
Following	O
is	O
an	O
example	O
program	O
in	O
Java	B-Language
language	I-Language
,	O
using	O
that	O
class	O
.	O
</s>
<s>
A	O
common	O
use	O
of	O
stacks	B-Application
at	O
the	O
architecture	O
level	O
is	O
as	O
a	O
means	O
of	O
allocating	O
and	O
accessing	O
memory	O
.	O
</s>
<s>
A	O
typical	O
stack	B-Application
is	O
an	O
area	O
of	O
computer	O
memory	O
with	O
a	O
fixed	O
origin	O
and	O
a	O
variable	O
size	O
.	O
</s>
<s>
Initially	O
the	O
size	O
of	O
the	O
stack	B-Application
is	O
zero	O
.	O
</s>
<s>
A	O
stack	B-Application
pointer	O
,	O
usually	O
in	O
the	O
form	O
of	O
a	O
hardware	O
register	O
,	O
points	O
to	O
the	O
most	O
recently	O
referenced	O
location	O
on	O
the	O
stack	B-Application
;	O
when	O
the	O
stack	B-Application
has	O
a	O
size	O
of	O
zero	O
,	O
the	O
stack	B-Application
pointer	O
points	O
to	O
the	O
origin	O
of	O
the	O
stack	B-Application
.	O
</s>
<s>
The	O
two	O
operations	O
applicable	O
to	O
all	O
stacks	B-Application
are	O
:	O
</s>
<s>
a	O
push	O
operation	O
:	O
the	O
address	O
in	O
the	O
stack	B-Application
pointer	O
is	O
adjusted	O
by	O
the	O
size	O
of	O
the	O
data	O
item	O
and	O
a	O
data	O
item	O
is	O
written	O
at	O
the	O
location	O
pointed	O
to	O
by	O
the	O
stack	B-Application
pointer	O
.	O
</s>
<s>
a	O
pop	O
or	O
pull	O
operation	O
:	O
a	O
data	O
item	O
at	O
the	O
current	O
location	O
pointed	O
to	O
by	O
the	O
stack	B-Application
pointer	O
is	O
read	O
and	O
the	O
stack	B-Application
pointer	O
is	O
adjusted	O
by	O
the	O
size	O
of	O
the	O
data	O
item	O
.	O
</s>
<s>
There	O
are	O
many	O
variations	O
on	O
the	O
basic	O
principle	O
of	O
stack	B-Application
operations	O
.	O
</s>
<s>
Every	O
stack	B-Application
has	O
a	O
fixed	O
location	O
,	O
in	O
memory	O
,	O
at	O
which	O
it	O
begins	O
.	O
</s>
<s>
As	O
data	O
items	O
are	O
added	O
to	O
the	O
stack	B-Application
,	O
the	O
stack	B-Application
pointer	O
is	O
displaced	O
to	O
indicate	O
the	O
current	O
extent	O
of	O
the	O
stack	B-Application
,	O
which	O
expands	O
away	O
from	O
the	O
origin	O
.	O
</s>
<s>
Stack	B-Application
pointers	O
may	O
point	O
to	O
the	O
origin	O
of	O
a	O
stack	B-Application
or	O
to	O
a	O
limited	O
range	O
of	O
addresses	O
either	O
above	O
or	O
below	O
the	O
origin	O
(	O
depending	O
on	O
the	O
direction	O
in	O
which	O
the	O
stack	B-Application
grows	O
)	O
;	O
however	O
,	O
the	O
stack	B-Application
pointer	O
cannot	O
cross	O
the	O
origin	O
of	O
the	O
stack	B-Application
.	O
</s>
<s>
In	O
other	O
words	O
,	O
if	O
the	O
origin	O
of	O
the	O
stack	B-Application
is	O
at	O
address	O
1000	O
and	O
the	O
stack	B-Application
grows	O
downwards	O
(	O
towards	O
addresses	O
999	O
,	O
998	O
,	O
and	O
so	O
on	O
)	O
,	O
the	O
stack	B-Application
pointer	O
must	O
never	O
be	O
incremented	O
beyond	O
1000	O
(	O
to	O
1001	O
,	O
1002	O
,	O
etc	O
.	O
)	O
.	O
</s>
<s>
If	O
a	O
pop	O
operation	O
on	O
the	O
stack	B-Application
causes	O
the	O
stack	B-Application
pointer	O
to	O
move	O
past	O
the	O
origin	O
of	O
the	O
stack	B-Application
,	O
a	O
stack	B-Application
underflow	I-Application
occurs	O
.	O
</s>
<s>
If	O
a	O
push	O
operation	O
causes	O
the	O
stack	B-Application
pointer	O
to	O
increment	O
or	O
decrement	O
beyond	O
the	O
maximum	O
extent	O
of	O
the	O
stack	B-Application
,	O
a	O
stack	B-Error_Name
overflow	I-Error_Name
occurs	O
.	O
</s>
<s>
Some	O
environments	O
that	O
rely	O
heavily	O
on	O
stacks	B-Application
may	O
provide	O
additional	O
operations	O
,	O
for	O
example	O
:	O
</s>
<s>
Peek	B-Application
:	O
the	O
topmost	O
item	O
is	O
inspected	O
(	O
or	O
returned	O
)	O
,	O
but	O
the	O
stack	B-Application
pointer	O
and	O
stack	B-Application
size	O
does	O
not	O
change	O
(	O
meaning	O
the	O
item	O
remains	O
on	O
the	O
stack	B-Application
)	O
.	O
</s>
<s>
Swap	O
or	O
exchange	O
:	O
the	O
two	O
topmost	O
items	O
on	O
the	O
stack	B-Application
exchange	O
places	O
.	O
</s>
<s>
Rotate	O
(	O
or	O
Roll	O
)	O
:	O
the	O
topmost	O
items	O
are	O
moved	O
on	O
the	O
stack	B-Application
in	O
a	O
rotating	O
fashion	O
.	O
</s>
<s>
For	O
example	O
,	O
if	O
,	O
items	O
1	O
,	O
2	O
,	O
and	O
3	O
on	O
the	O
stack	B-Application
are	O
moved	O
to	O
positions	O
2	O
,	O
3	O
,	O
and	O
1	O
on	O
the	O
stack	B-Application
,	O
respectively	O
.	O
</s>
<s>
Stacks	B-Application
are	O
often	O
visualized	O
growing	O
from	O
the	O
bottom	O
up	O
(	O
like	O
real-world	O
stacks	B-Application
)	O
.	O
</s>
<s>
The	O
important	O
feature	O
is	O
that	O
the	O
bottom	O
of	O
the	O
stack	B-Application
is	O
in	O
a	O
fixed	O
position	O
.	O
</s>
<s>
The	O
illustration	O
in	O
this	O
section	O
is	O
an	O
example	O
of	O
a	O
top-to-bottom	O
growth	O
visualization	O
:	O
the	O
top	O
(	O
28	O
)	O
is	O
the	O
stack	B-Application
"	O
bottom	O
"	O
,	O
since	O
the	O
stack	B-Application
"	O
top	O
"	O
(	O
9	O
)	O
is	O
where	O
items	O
are	O
pushed	O
or	O
popped	O
from	O
.	O
</s>
<s>
A	O
stack	B-Application
is	O
usually	O
represented	O
in	O
computers	O
by	O
a	O
block	O
of	O
memory	O
cells	O
,	O
with	O
the	O
"	O
bottom	O
"	O
at	O
a	O
fixed	O
location	O
,	O
and	O
the	O
stack	B-Application
pointer	O
holding	O
the	O
address	O
of	O
the	O
current	O
"	O
top	O
"	O
cell	O
in	O
the	O
stack	B-Application
.	O
</s>
<s>
The	O
top	O
and	O
bottom	O
terminology	O
are	O
used	O
irrespective	O
of	O
whether	O
the	O
stack	B-Application
actually	O
grows	O
towards	O
lower	O
memory	O
addresses	O
or	O
towards	O
higher	O
memory	O
addresses	O
.	O
</s>
<s>
Pushing	O
an	O
item	O
on	O
to	O
the	O
stack	B-Application
adjusts	O
the	O
stack	B-Application
pointer	O
by	O
the	O
size	O
of	O
the	O
item	O
(	O
either	O
decrementing	O
or	O
incrementing	O
,	O
depending	O
on	O
the	O
direction	O
in	O
which	O
the	O
stack	B-Application
grows	O
in	O
memory	O
)	O
,	O
pointing	O
it	O
to	O
the	O
next	O
cell	O
,	O
and	O
copies	O
the	O
new	O
top	O
item	O
to	O
the	O
stack	B-Application
area	O
.	O
</s>
<s>
Depending	O
again	O
on	O
the	O
exact	O
implementation	O
,	O
at	O
the	O
end	O
of	O
a	O
push	O
operation	O
,	O
the	O
stack	B-Application
pointer	O
may	O
point	O
to	O
the	O
next	O
unused	O
location	O
in	O
the	O
stack	B-Application
,	O
or	O
it	O
may	O
point	O
to	O
the	O
topmost	O
item	O
in	O
the	O
stack	B-Application
.	O
</s>
<s>
If	O
the	O
stack	B-Application
points	O
to	O
the	O
current	O
topmost	O
item	O
,	O
the	O
stack	B-Application
pointer	O
will	O
be	O
updated	O
before	O
a	O
new	O
item	O
is	O
pushed	O
onto	O
the	O
stack	B-Application
;	O
if	O
it	O
points	O
to	O
the	O
next	O
available	O
location	O
in	O
the	O
stack	B-Application
,	O
it	O
will	O
be	O
updated	O
after	O
the	O
new	O
item	O
is	O
pushed	O
onto	O
the	O
stack	B-Application
.	O
</s>
<s>
Popping	O
the	O
stack	B-Application
is	O
simply	O
the	O
inverse	O
of	O
pushing	O
.	O
</s>
<s>
The	O
topmost	O
item	O
in	O
the	O
stack	B-Application
is	O
removed	O
and	O
the	O
stack	B-Application
pointer	O
is	O
updated	O
,	O
in	O
the	O
opposite	O
order	O
of	O
that	O
used	O
in	O
the	O
push	O
operation	O
.	O
</s>
<s>
Many	O
CISC-type	O
CPU	B-General_Concept
designs	O
,	O
including	O
the	O
x86	B-Operating_System
,	O
Z80	B-General_Concept
and	O
6502	B-General_Concept
,	O
have	O
a	O
dedicated	O
register	O
for	O
use	O
as	O
the	O
call	B-General_Concept
stack	I-General_Concept
stack	B-Application
pointer	O
with	O
dedicated	O
call	O
,	O
return	O
,	O
push	O
,	O
and	O
pop	O
instructions	O
that	O
implicitly	O
update	O
the	O
dedicated	O
register	O
,	O
thus	O
increasing	O
code	B-General_Concept
density	O
.	O
</s>
<s>
Some	O
CISC	B-Architecture
processors	I-Architecture
,	O
like	O
the	O
PDP-11	B-Device
and	O
the	O
68000	B-Device
,	O
also	O
have	O
special	O
addressing	O
modes	O
for	O
implementation	O
of	O
stacks	B-Application
,	O
typically	O
with	O
a	O
semi-dedicated	O
stack	B-Application
pointer	O
as	O
well	O
(	O
such	O
as	O
A7	O
in	O
the	O
68000	B-Device
)	O
.	O
</s>
<s>
In	O
contrast	O
,	O
most	O
RISC	B-Architecture
CPU	B-General_Concept
designs	O
do	O
not	O
have	O
dedicated	O
stack	B-Application
instructions	O
and	O
therefore	O
most	O
,	O
if	O
not	O
all	O
,	O
registers	O
may	O
be	O
used	O
as	O
stack	B-Application
pointers	O
as	O
needed	O
.	O
</s>
<s>
Some	O
machines	O
use	O
a	O
stack	B-Application
for	O
arithmetic	O
and	O
logical	O
operations	O
;	O
operands	O
are	O
pushed	O
onto	O
the	O
stack	B-Application
,	O
and	O
arithmetic	O
and	O
logical	O
operations	O
act	O
on	O
the	O
top	O
one	O
or	O
more	O
items	O
on	O
the	O
stack	B-Application
,	O
popping	O
them	O
off	O
the	O
stack	B-Application
and	O
pushing	O
the	O
result	O
onto	O
the	O
stack	B-Application
.	O
</s>
<s>
Machines	O
that	O
function	O
in	O
this	O
fashion	O
are	O
called	O
stack	B-Application
machines	I-Application
.	O
</s>
<s>
A	O
number	O
of	O
mainframes	B-Architecture
and	O
minicomputers	B-Architecture
were	O
stack	B-Application
machines	I-Application
,	O
the	O
most	O
famous	O
being	O
the	O
Burroughs	B-Device
large	I-Device
systems	I-Device
.	O
</s>
<s>
Other	O
examples	O
include	O
the	O
CISC	B-Architecture
HP	B-Device
3000	I-Device
machines	O
and	O
the	O
CISC	B-Architecture
machines	O
from	O
Tandem	O
Computers	O
.	O
</s>
<s>
The	O
x87	B-Application
floating	B-Algorithm
point	I-Algorithm
architecture	O
is	O
an	O
example	O
of	O
a	O
set	O
of	O
registers	O
organised	O
as	O
a	O
stack	B-Application
where	O
direct	O
access	O
to	O
individual	O
registers	O
(	O
relative	O
to	O
the	O
current	O
top	O
)	O
is	O
also	O
possible	O
.	O
</s>
<s>
Having	O
the	O
top-of-stack	O
as	O
an	O
implicit	O
argument	O
allows	O
for	O
a	O
small	O
machine	B-Language
code	I-Language
footprint	O
with	O
a	O
good	O
usage	O
of	O
bus	B-General_Concept
bandwidth	O
and	O
code	B-General_Concept
caches	B-General_Concept
,	O
but	O
it	O
also	O
prevents	O
some	O
types	O
of	O
optimizations	O
possible	O
on	O
processors	O
permitting	O
random	B-General_Concept
access	I-General_Concept
to	O
the	O
register	B-General_Concept
file	I-General_Concept
for	O
all	O
(	O
two	O
or	O
three	O
)	O
operands	O
.	O
</s>
<s>
A	O
stack	B-Application
structure	O
also	O
makes	O
superscalar	B-General_Concept
implementations	O
with	O
register	B-Architecture
renaming	I-Architecture
(	O
for	O
speculative	B-General_Concept
execution	I-General_Concept
)	O
somewhat	O
more	O
complex	O
to	O
implement	O
,	O
although	O
it	O
is	O
still	O
feasible	O
,	O
as	O
exemplified	O
by	O
modern	O
x87	B-Application
implementations	O
.	O
</s>
<s>
Sun	B-Architecture
SPARC	I-Architecture
,	O
AMD	B-General_Concept
Am29000	I-General_Concept
,	O
and	O
Intel	B-General_Concept
i960	I-General_Concept
are	O
all	O
examples	O
of	O
architectures	O
using	O
register	B-General_Concept
windows	I-General_Concept
within	O
a	O
register-stack	O
as	O
another	O
strategy	O
to	O
avoid	O
the	O
use	O
of	O
slow	O
main	O
memory	O
for	O
function	O
arguments	O
and	O
return	O
values	O
.	O
</s>
<s>
There	O
are	O
also	O
a	O
number	O
of	O
small	O
microprocessors	O
that	O
implements	O
a	O
stack	B-Application
directly	O
in	O
hardware	O
and	O
some	O
microcontrollers	B-Architecture
have	O
a	O
fixed-depth	O
stack	B-Application
that	O
is	O
not	O
directly	O
accessible	O
.	O
</s>
<s>
Examples	O
are	O
the	O
PIC	B-Architecture
microcontrollers	I-Architecture
,	O
the	O
Computer	O
Cowboys	O
MuP21	O
,	O
the	O
Harris	B-Application
RTX	I-Application
line	O
,	O
and	O
the	O
Novix	O
NC4016	O
.	O
</s>
<s>
Many	O
stack-based	B-Language
microprocessors	O
were	O
used	O
to	O
implement	O
the	O
programming	O
language	O
Forth	B-Application
at	O
the	O
microcode	B-Device
level	O
.	O
</s>
<s>
Calculators	O
employing	O
reverse	B-Application
Polish	I-Application
notation	O
use	O
a	O
stack	B-Application
structure	O
to	O
hold	O
values	O
.	O
</s>
<s>
Expressions	O
can	O
be	O
represented	O
in	O
prefix	O
,	O
postfix	O
or	O
infix	O
notations	O
and	O
conversion	O
from	O
one	O
form	O
to	O
another	O
may	O
be	O
accomplished	O
using	O
a	O
stack	B-Application
.	O
</s>
<s>
Many	O
compilers	O
use	O
a	O
stack	B-Application
for	O
parsing	O
the	O
syntax	O
of	O
expressions	O
,	O
program	O
blocks	O
etc	O
.	O
</s>
<s>
before	O
translating	O
into	O
low-level	O
code	B-General_Concept
.	O
</s>
<s>
Most	O
programming	O
languages	O
are	O
context-free	O
languages	O
,	O
allowing	O
them	O
to	O
be	O
parsed	O
with	O
stack-based	B-Language
machines	O
.	O
</s>
<s>
Another	O
important	O
application	O
of	O
stacks	B-Application
is	O
backtracking	B-Algorithm
.	O
</s>
<s>
This	O
can	O
be	O
done	O
with	O
the	O
use	O
of	O
stacks	B-Application
.	O
</s>
<s>
With	O
the	O
help	O
of	O
stacks	B-Application
,	O
we	O
remember	O
the	O
point	O
where	O
we	O
have	O
reached	O
.	O
</s>
<s>
This	O
is	O
done	O
by	O
pushing	O
that	O
point	O
into	O
the	O
stack	B-Application
.	O
</s>
<s>
In	O
case	O
we	O
end	O
up	O
on	O
the	O
wrong	O
path	O
,	O
we	O
can	O
pop	O
the	O
last	O
point	O
from	O
the	O
stack	B-Application
and	O
thus	O
return	O
to	O
the	O
last	O
point	O
and	O
continue	O
our	O
quest	O
to	O
find	O
the	O
right	O
path	O
.	O
</s>
<s>
This	O
is	O
called	O
backtracking	B-Algorithm
.	O
</s>
<s>
The	O
prototypical	O
example	O
of	O
a	O
backtracking	B-Algorithm
algorithm	I-Algorithm
is	O
depth-first	B-Algorithm
search	I-Algorithm
,	O
which	O
finds	O
all	O
vertices	O
of	O
a	O
graph	O
that	O
can	O
be	O
reached	O
from	O
a	O
specified	O
starting	O
vertex	O
.	O
</s>
<s>
Other	O
applications	O
of	O
backtracking	B-Algorithm
involve	O
searching	O
through	O
spaces	O
that	O
represent	O
potential	O
solutions	O
to	O
an	O
optimization	O
problem	O
.	O
</s>
<s>
Branch	B-Algorithm
and	I-Algorithm
bound	I-Algorithm
is	O
a	O
technique	O
for	O
performing	O
such	O
backtracking	B-Algorithm
searches	I-Algorithm
without	O
exhaustively	O
searching	O
all	O
of	O
the	O
potential	O
solutions	O
in	O
such	O
a	O
space	O
.	O
</s>
<s>
A	O
number	O
of	O
programming	O
languages	O
are	O
stack-oriented	B-Language
,	O
meaning	O
they	O
define	O
most	O
basic	O
operations	O
(	O
adding	O
two	O
numbers	O
,	O
printing	O
a	O
character	O
)	O
as	O
taking	O
their	O
arguments	O
from	O
the	O
stack	B-Application
,	O
and	O
placing	O
any	O
return	O
values	O
back	O
on	O
the	O
stack	B-Application
.	O
</s>
<s>
For	O
example	O
,	O
PostScript	B-Language
has	O
a	O
return	O
stack	B-Application
and	O
an	O
operand	O
stack	B-Application
,	O
and	O
also	O
has	O
a	O
graphics	O
state	O
stack	B-Application
and	O
a	O
dictionary	O
stack	B-Application
.	O
</s>
<s>
Many	O
virtual	B-Architecture
machines	I-Architecture
are	O
also	O
stack-oriented	B-Language
,	O
including	O
the	O
p-code	B-Language
machine	I-Language
and	O
the	O
Java	B-Language
Virtual	I-Language
Machine	I-Language
.	O
</s>
<s>
Almost	O
all	O
calling	O
conventionsthe	O
ways	O
in	O
which	O
subroutines	O
receive	O
their	O
parameters	O
and	O
return	O
resultsuse	O
a	O
special	O
stack	B-Application
(	O
the	O
"	O
call	B-General_Concept
stack	I-General_Concept
"	O
)	O
to	O
hold	O
information	O
about	O
procedure/function	O
calling	O
and	O
nesting	O
in	O
order	O
to	O
switch	O
to	O
the	O
context	O
of	O
the	O
called	O
function	O
and	O
restore	O
to	O
the	O
caller	O
function	O
when	O
the	O
calling	O
finishes	O
.	O
</s>
<s>
The	O
functions	O
follow	O
a	O
runtime	O
protocol	O
between	O
caller	O
and	O
callee	O
to	O
save	O
arguments	O
and	O
return	O
value	O
on	O
the	O
stack	B-Application
.	O
</s>
<s>
Stacks	B-Application
are	O
an	O
important	O
way	O
of	O
supporting	O
nested	O
or	O
recursive	O
function	O
calls	O
.	O
</s>
<s>
This	O
type	O
of	O
stack	B-Application
is	O
used	O
implicitly	O
by	O
the	O
compiler	O
to	O
support	O
CALL	O
and	O
RETURN	O
statements	O
(	O
or	O
their	O
equivalents	O
)	O
and	O
is	O
not	O
manipulated	O
directly	O
by	O
the	O
programmer	O
.	O
</s>
<s>
Some	O
programming	O
languages	O
use	O
the	O
stack	B-Application
to	O
store	O
data	O
that	O
is	O
local	O
to	O
a	O
procedure	O
.	O
</s>
<s>
Space	O
for	O
local	O
data	O
items	O
is	O
allocated	O
from	O
the	O
stack	B-Application
when	O
the	O
procedure	O
is	O
entered	O
,	O
and	O
is	O
deallocated	O
when	O
the	O
procedure	O
exits	O
.	O
</s>
<s>
The	O
C	B-Language
programming	I-Language
language	I-Language
is	O
typically	O
implemented	O
in	O
this	O
way	O
.	O
</s>
<s>
Using	O
the	O
same	O
stack	B-Application
for	O
both	O
data	O
and	O
procedure	O
calls	O
has	O
important	O
security	O
implications	O
(	O
see	O
below	O
)	O
of	O
which	O
a	O
programmer	O
must	O
be	O
aware	O
in	O
order	O
to	O
avoid	O
introducing	O
serious	O
security	O
bugs	O
into	O
a	O
program	O
.	O
</s>
<s>
Several	O
algorithms	O
use	O
a	O
stack	B-Application
(	O
separate	O
from	O
the	O
usual	O
function	O
call	B-General_Concept
stack	I-General_Concept
of	O
most	O
programming	O
languages	O
)	O
as	O
the	O
principal	O
data	B-General_Concept
structure	I-General_Concept
with	O
which	O
they	O
organize	O
their	O
information	O
.	O
</s>
<s>
Graham	B-Algorithm
scan	I-Algorithm
,	O
an	O
algorithm	O
for	O
the	O
convex	O
hull	O
of	O
a	O
two-dimensional	O
system	O
of	O
points	O
.	O
</s>
<s>
A	O
convex	O
hull	O
of	O
a	O
subset	O
of	O
the	O
input	O
is	O
maintained	O
in	O
a	O
stack	B-Application
,	O
which	O
is	O
used	O
to	O
find	O
and	O
remove	O
concavities	O
in	O
the	O
boundary	O
when	O
a	O
new	O
point	O
is	O
added	O
to	O
the	O
hull	O
.	O
</s>
<s>
Part	O
of	O
the	O
SMAWK	B-Algorithm
algorithm	I-Algorithm
for	O
finding	O
the	O
row	O
minima	O
of	O
a	O
monotone	O
matrix	O
uses	O
stacks	B-Application
in	O
a	O
similar	O
way	O
to	O
Graham	B-Algorithm
scan	I-Algorithm
.	O
</s>
<s>
All	B-Operating_System
nearest	I-Operating_System
smaller	I-Operating_System
values	I-Operating_System
,	O
the	O
problem	O
of	O
finding	O
,	O
for	O
each	O
number	O
in	O
an	O
array	B-Data_Structure
,	O
the	O
closest	O
preceding	O
number	O
that	O
is	O
smaller	O
than	O
it	O
.	O
</s>
<s>
One	O
algorithm	O
for	O
this	O
problem	O
uses	O
a	O
stack	B-Application
to	O
maintain	O
a	O
collection	B-Application
of	O
candidates	O
for	O
the	O
nearest	O
smaller	O
value	O
.	O
</s>
<s>
For	O
each	O
position	O
in	O
the	O
array	B-Data_Structure
,	O
the	O
stack	B-Application
is	O
popped	O
until	O
a	O
smaller	O
value	O
is	O
found	O
on	O
its	O
top	O
,	O
and	O
then	O
the	O
value	O
in	O
the	O
new	O
position	O
is	O
pushed	O
onto	O
the	O
stack	B-Application
.	O
</s>
<s>
The	O
nearest-neighbor	B-Algorithm
chain	I-Algorithm
algorithm	I-Algorithm
,	O
a	O
method	O
for	O
agglomerative	B-Algorithm
hierarchical	I-Algorithm
clustering	I-Algorithm
based	O
on	O
maintaining	O
a	O
stack	B-Application
of	O
clusters	O
,	O
each	O
of	O
which	O
is	O
the	O
nearest	O
neighbor	O
of	O
its	O
predecessor	O
on	O
the	O
stack	B-Application
.	O
</s>
<s>
Some	O
computing	O
environments	O
use	O
stacks	B-Application
in	O
ways	O
that	O
may	O
make	O
them	O
vulnerable	O
to	O
security	O
breaches	O
and	O
attacks	O
.	O
</s>
<s>
For	O
example	O
,	O
some	O
programming	O
languages	O
use	O
a	O
common	O
stack	B-Application
to	O
store	O
both	O
data	O
local	O
to	O
a	O
called	O
procedure	O
and	O
the	O
linking	O
information	O
that	O
allows	O
the	O
procedure	O
to	O
return	O
to	O
its	O
caller	O
.	O
</s>
<s>
This	O
means	O
that	O
the	O
program	O
moves	O
data	O
into	O
and	O
out	O
of	O
the	O
same	O
stack	B-Application
that	O
contains	O
critical	O
return	O
addresses	O
for	O
the	O
procedure	O
calls	O
.	O
</s>
<s>
If	O
data	O
is	O
moved	O
to	O
the	O
wrong	O
location	O
on	O
the	O
stack	B-Application
,	O
or	O
an	O
oversized	O
data	O
item	O
is	O
moved	O
to	O
a	O
stack	B-Application
location	O
that	O
is	O
not	O
large	O
enough	O
to	O
contain	O
it	O
,	O
return	O
information	O
for	O
procedure	O
calls	O
may	O
be	O
corrupted	O
,	O
causing	O
the	O
program	O
to	O
fail	O
.	O
</s>
<s>
Malicious	O
parties	O
may	O
attempt	O
a	O
stack	B-General_Concept
smashing	I-General_Concept
attack	O
that	O
takes	O
advantage	O
of	O
this	O
type	O
of	O
implementation	O
by	O
providing	O
oversized	O
data	O
input	O
to	O
a	O
program	O
that	O
does	O
not	O
check	O
the	O
length	O
of	O
input	O
.	O
</s>
<s>
Such	O
a	O
program	O
may	O
copy	O
the	O
data	O
in	O
its	O
entirety	O
to	O
a	O
location	O
on	O
the	O
stack	B-Application
,	O
and	O
in	O
so	O
doing	O
it	O
may	O
change	O
the	O
return	O
addresses	O
for	O
procedures	O
that	O
have	O
called	O
it	O
.	O
</s>
<s>
An	O
attacker	O
can	O
experiment	O
to	O
find	O
a	O
specific	O
type	O
of	O
data	O
that	O
can	O
be	O
provided	O
to	O
such	O
a	O
program	O
such	O
that	O
the	O
return	O
address	O
of	O
the	O
current	O
procedure	O
is	O
reset	O
to	O
point	O
to	O
an	O
area	O
within	O
the	O
stack	B-Application
itself	O
(	O
and	O
within	O
the	O
data	O
provided	O
by	O
the	O
attacker	O
)	O
,	O
which	O
in	O
turn	O
contains	O
instructions	O
that	O
carry	O
out	O
unauthorized	O
operations	O
.	O
</s>
<s>
This	O
type	O
of	O
attack	O
is	O
a	O
variation	O
on	O
the	O
buffer	B-General_Concept
overflow	I-General_Concept
attack	O
and	O
is	O
an	O
extremely	O
frequent	O
source	O
of	O
security	O
breaches	O
in	O
software	O
,	O
mainly	O
because	O
some	O
of	O
the	O
most	O
popular	O
compilers	O
use	O
a	O
shared	O
stack	B-Application
for	O
both	O
data	O
and	O
procedure	O
calls	O
,	O
and	O
do	O
not	O
verify	O
the	O
length	O
of	O
data	O
items	O
.	O
</s>
<s>
Frequently	O
,	O
programmers	O
do	O
not	O
write	O
code	B-General_Concept
to	O
verify	O
the	O
size	O
of	O
data	O
items	O
,	O
either	O
,	O
and	O
when	O
an	O
oversized	O
or	O
undersized	O
data	O
item	O
is	O
copied	O
to	O
the	O
stack	B-Application
,	O
a	O
security	O
breach	O
may	O
occur	O
.	O
</s>
