L10 - Stack Organization - Unit 1
L10 - Stack Organization - Unit 1
(An ISO – 9001: 2008 Certified & ‘A’ Grade accredited Institution by NAAC)
Stack Organization
Topics Covered:
• Introduction of Stack
• Operations to be performed onto the stack
• Stack pointer movement through the Stack
• Types of the Stack
• List of some applications of stack
Last-in–First-out (LIFO) Principle: The last data item placed on the stack is the
first one removed when retrieval begins. In other way, we may call it as First-in-
Last out (FILO) principle; the first data item placed on the stack will be
removed in the last.
The structure is sometimes referred to as a pushdown stack.
How to work with Stack? Or
What are the operations to be performed onto the stack?
The terms push and pop are used to describe placing a new item on the stack
and removing the top item from the stack, respectively.
Push: To insert an element into Stack
Pop: To remove the topmost element from the stack
A register is used to store the address of the topmost element of the stack which
is known as Stack pointer (SP). The stack pointer register SP contains a binary
number whose value is equal to the address of the element that is currently on
top of the stack.
Register Stack:
• A stack can be placed in a portion of a large memory or it can be
organized as a collection of a finite number of memory words or
registers.
• Two one-bit registers namely FULL and EMPTY will be used to get the
status of stack such as whether the stack if full (containing all the
elements according to the capacity) or empty (containing no element).
• Numbering for N registers in a register stack: 0 to (N-1).
Example: the registers will be number from 0 to 7 in 8-register stack.
FULL is a one-bit flag register for showing the status of stack whether it is Full
(containing all 64 elements) or not.
• FULL=0 (Stack is not full)
• FULL=1 (Stack is full)
EMPTY is also a one-bit flag register for showing the status of stack whether it
is Empty (containing no element) or not
• EMPTY =0 (Stack is Not empty)
• EMPTY = 1 (Stack is Empty)
DR is the data register that holds the binary data to be written in to or read out
of the stack.
Initially:
• FULL=0
• EMPTY=1
• SP=0
The above figure shows the organization of a 64-word register stack. The stack
pointer register SP contains a binary number whose value is equal to the address
of the element that is currently on top of the stack.
2k = N
K= ⌊log2 (N) ⌋ + 1
Note:
The first item stored in the stack is at address 1.
The second last (or prior to last) item stored in the stack is at address 63.
The last item is stored at address 0
Initially, SP points to 0. The first item stored in the stack is at address 1. The
last item is stored at address 0, if SP reaches 0; the stack is full of item, so
FULL is set to 1. This condition is reached if the top item prior to the last push
was in location 63 and after increment SP, the last item stored in location 0.
Once an item is stored in location 0, there are no more empty register in the
stack. If an item is written in the stack, obviously the stack cannot be empty, so
EMTY is cleared to 0.
The following figure shows the Stack after inserting item ‘A’
The following figure shows the Stack after inserting item ‘B’
The following figure shows the Stack after inserting item ‘C’
Three items are placed in the stack: A, B, and C in the order. Item C is on the
top of the stack so the SP points to address 3.
How does a stack pointer move across the ends of the stack?
How does SP points to address 0 from address 63 after incrementing SP by 1?
In a 64-word stack, the stack pointer contains 6 bits because 26 =64.
Since SP has only six bits, it cannot exceed a number greater than 63 (111111 in
binary).
When after 63, SP is incremented by 1, the result is 0 since 111111 + 1 =1000000 in
binary according to the binary addition, but SP can accommodate only the six least
significant bits i.e. SP=000000.
How does SP points to address 63 from address 0 after decrementing by 1?
SP =0= 000000 (in binary)
SP=SP-1 =000000-1
The subtraction in digital computer system will be done using 2’s complement method.
So, the subtraction will be converted into binary addition like
SP = SP + (-1)
SP = SP + 2’s complement (+1)
= 000000 + 111111
= 111111
=63
1. To remove the top item, the stack is popped by reading the memory word at
address 3 and decrementing the content of SP. Item B is now on top of the stack
since SP holds address
2. Note that item C has read out but not physically removed. But it is logically
out of the stack. This does not matter because when the stack is pushed, a new
item is written in its place.
Memory Stack:
A stack can exist as a stand-alone unit or can be implemented in a random
access memory attached to CPU. The implementation of a stack in the CPU is
done by assigning a portion of memory to a stack operation and using a
processor register as a Stack Pointer.
Figure given below shows a portion of computer memory partitioned in to three
segments program, data and stack.
The Program Counter PC points at the address of the next instruction in the
program.
The Address Register AR points at an array of data.
The Stack Pointer SP points at the top of the stack.
The three register 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.
The initial value of SP is 4001 and the stack grows with decreasing addresses.
Thus the first item stored in the stack is at address 4000, the second item is
stored at address 3999, and the last address that can be used for the stack is
3000. No provisions are available for stack limit checks. We assume that the
items in the stack communicate with a data register DR.
Push Operation:
A new item is inserted with the push operation as follows.
SP← SP-1 (Decrement stack Pointer)
M [SP] ← DR (Write item on top of the stack)
The stack pointer is decremented so that it points at the address of the next
word. A Memory write operation inserts the word from DR into the top of the
stack.
Pop Operation:
A new item is deleted with a pop operation as follows.
DR← M [SP] (Read item from the top of stack)
SP←SP + 1 (increment stack Pointer)
The top item is read from the stack in to DR. The stack pointer is then
incremented to point at the next item in the stack.
Most computers do not provide hardware to check for stack overflow (FULL) or
underflow (Empty). The stack limit can be checked by using two processor
register:
One to hold upper limit and other hold the lower limit. After the pop or push
operation SP is compared with lower or upper limit register.
Note:
In case of memory stack, stack may grow in direction of increasing
addresses or decreasing addresses of memory. It depends on the design of
the system.
What are the applications of a Stack in a computer system?
There are the following applications in computer architecture.
• Subroutines Call
• Zero-address Instruction Implementation
• Infix notation to Post-fix notation conversion
• Evaluation of Arithmetic Expressions