Computer Architecture &
Assembly Language
Topic (Stack & its operations)
The Stack
A Stack is a data structure that works on LIFO principal
The stack is a LIFO structure – Last In First Out
The stack is an area of memory identified by the programmer for
temporary storage of information
The programmer defines the
bottom of the stack and the stack
grows up into reducing address
range
Why do we use Stack in Assembly
Language
Swap two Numbers
To reverse a string
Helps in Nested Loops (Loops with in the Loop)
How to use Stack in Assembly Program
A Stack is basically a reserved part in Memory
We use Stack by Directive .Stack 100h
It’s a directive / command that reserves 100h bytes
for stack
Stack Segment Register
Hold Address of Space reserved for stack
Stack Pointer Register
Point Top of Space reserved for stack
SS:SP
How to use Stack in Assembly Program
Example:
In the 8085, the stack is defined by setting the SP (Stack Pointer)
register
LX SP, FFFFH
It loads address on the Stack Pointer (SP)
This sets the Stack Pointer to location FFFFH (end of memory for
the 8085)
Saving Information on the Stack
Information is saved on the stack by PUSHing it on
It is retrieved from the stack by POPing it off
The 8085 provides two instructions:
PUSH and POP for storing information on the stack and
retrieving it back.
How to add value in Stack
We use function Push to add values in stack
PUSH Register/Variable
Example 100
MOV AX,2 .
.
PUSH AX .
199
200 SP
Decrement SP
Copy the contents of register AX to the memory
location pointed to by SP
Copies content from Operand to top of stack
How to take out value from Stack
We use function Pop to take out values from stack
POP Register/Variable
101
Example .
.
POP AX .
199
200 SP
Increment SP 201
Copy the contents of the memory location pointed by SP
Copies content from top of stack to Operand to register AX
Operation of the Stack
During PUSHing, the stack operates in a “decrement then store” style
– The stack pointer is decremented first, then the
information is placed on the stack.
During POPing, the stack operates in a “use then increment” style.
– The information is retrieved from the top of the the stack and then
the pointer is incremented
The SP pointer always points to “the top of the stack”
LIFO
The order of PUSHs and POPs must be opposite of each
other in order to retrieve information back into its original location.
PUSH B
PUSH D
...
POP D
POP B
Reversing the order of the POP instructions will result in
the exchange of the contents