Stack in 8085
Programmer's view of 8085
Introduction to Stack
• The stack is a reserved area of the memory
(identified by the programmer) where we can
store temporary information.
• The stack is a LIFO structure.
• The stack normally grows backwards into
memory.
How does a stack work in assembly
language?
• The 8085 provides two instructions: PUSH and
POP for storing information on the stack and
retrieving it back.
– Both PUSH and POP work with register
pairs ONLY.
Operation of the Stack
• During pushing, the stack operates in a
decrement then store style.
• During poping, the stack operates in a use
then increment style.
• 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
The PUSH Instruction
• PUSH B (1 Byte Instruction) Rp
– Decrement SP
– Copy the contents of register B to the
memory location pointed to by SP
– Decrement SP
– Copy the contents of register C to the
memory location pointed to by SP
12B
CF3
FFFB
FFFC
FFFD F3
FFFE 12
FFFF SP
The POP Instruction
• POP D (1 Byte Instruction)
– Copy the contents of the memory location
pointed to by the SP to register E
– Increment SP
– Copy the contents of the memory location
pointed to by the SP to register D
– Increment SP
D E
12 F3
FFFB
FFFC
FFFD F3 SP
FFFE 12
FFFF
Saving Information on the Stack
• There are two methods to add data to the
stack:
– Direct method
– Indirect method
Saving Information on the Stack
• Direct method
– the stack pointers address is loaded into the
stack pointer register directly
LXI SP,8000H
LXI H,1234H
PUSH H
POP D
HLT
Saving Information on the Stack
• Indirect method
– the stack pointer’s address is loaded into the
stack pointer register via another register pair.
LXI H,8000H
SPHL
LXI H,1234H
PUSH H
POP D
HLT
PSW Register Pair
• The 8085 recognizes one additional register
pair called the PSW (Program Status Word).
– This register pair is made up of the
Accumulator and the Flags registers.
• It is possible to push the PSW onto the stack,
do whatever operations are needed, then POP
it off of the stack.
PUSH PSW Register Pair
• PUSH PSW (1 Byte Instruction)
– Decrement SP
– Copy the contents of register A to the memory location
pointed to by SP
– Decrement SP
– Copy the contents of Flag register to the memory
location pointed to by SP
Pop PSW Register Pair
• POP PSW (1 Byte Instruction)
– Copy the contents of the memory location
pointed to by the SP to Flag register
– Increment SP
– Copy the contents of the memory location
pointed to by the SP to register A
– Increment SP
Modify Flag Content using PUSH/POP
• We want to Reset the Zero Flag