Stacks are data structures that follow LIFO (last in, first out) ordering. Elements can only be added to or removed from one end, called the top. Stacks are used to maintain information between main programs and subroutines. Elements are pushed onto the stack by decrementing the stack pointer and storing the element, and popped off by incrementing the stack pointer after retrieving the element. Operations must check that the stack is not empty before popping or full before pushing to prevent errors.
A computer oftenneed to perform a particular
subtask using the familiar subroutine structure.
In order to maintain information and linkage
between main program and subroutine, a data
structure called stack is used.
STACK
Astack is a list of data elements, usually
words or bytes, with accessing restriction
that elements can be added (or) removed at
one end of the list is called top of the stack.
Other end is called bottom of the stack.
The structure is sometimes referred to as a
pushdown stack.
Another descriptive
LIFO-thelast data placed on the stack is the
first one removed when retrieval begins.
Push & Pop- used to placing a new item on
the stack and removing the top item from the
stack.
7.
Assume data storedin stack
First element is placed in location
BOTTOM, when new elements are pushed
onto the stack, they are store successive
lower address locations.
We use a stack that grows in the direction of
decreasing memory addresses.
8.
example
Stack ofword items in the memory of a
computer.
It contain numerical values, with 43 at the
bottom and -28 at the top.
SP- A processor register point the top of the
stack.
32 bit word length and byte addressable.
10.
Push operation
Subtract#4, SP
Move NEWITEM, (SP)
The subtract instruction subtracts the source operand 4
from the destination operand contained in SP and place
the result in SP.
These two instructions move the word from location
NEWITEM onto the top of the stack, by decrementing
the stack pointer by 4 before the move.
12.
POP operation
Move (SP), ITEM
Add #4,SP
These two instruction move the top value
from stack into location ITEM and then
increment the stack pointer by 4, now SP
point to the new top element.
13.
Auto increment ordecrement
Move NEWITEM, -(SP) -Push operation
Move (SP)+, ITEM - POP operation
14.
concept
When stackis used in a program, it is
usually allocated a fixed amount of space in
the memory.
We must avoid push an item onto the stack
when the stack has reached its max size.
We also avoid pop an item when the stack
off an empty stack.
Which could result from a programming
error.
15.
example
Stack runsfrom location – 2000(Bottom)
And no further than location 1500
Recall that SP is decremented by 4 before
new data are stored on the stack.
Hence, an initial value of 2004 means that
the first item pushed onto the stack will be at
location 2000.