2. STACK
The stack is an area of memory
identified by the programmer for
temporary storage of information.
Address of the stack is stored into the
stack pointer register.
The stack is a LIFO structure. – Last In
First Out.
The starting location of the stack is
defined by loading a 16 bit address into
the stack pointer that spaced is
reserved, usually at the top of the
memory map.
2SSP/EC-502/2020
3. STACK Cont…
The stack normally grows backwards into
memory. – i.e. the programmer defines the
bottom of the stack and the stack grows up
into reducing address range.
The stack can be initialized
anywhere in the user memory
map , but stack is initialized at
the highest memory location
so that there will not be any
interface with the program.
3SSP/EC-502/2020
4. STACK Cont…
o The Size of the stack is limited only
by the available memory
o In the 8085, the stack is defined by
setting the SP (Stack Pointer) register.
LXI SP, F653H
o The LXI SP,F653 state that load the 16
bit address into the stack pointer
register.
4SSP/EC-502/2020
5. Information is stored and retrieved from
the stack
• The 8085 provide two instruction PUSH & POP for
storing information on the stack and retrieving it back.
• Information in the register pairs stored on the stack in
reverse order by using the instruction PUSH.
• Information retrieved from the stack by using the
instruction POP.
• PUSH & POP both instruction works with register pairs
only.
• The storage and retrieval of the content of registers on
the stack follows the LIFO(Last-In-First-Out)
sequence.
• Information in the stack location may not be destroyed
until new information is stored in that memory5SSP/EC-502/2020
6. Operation of stack by PUSH and POP
instruction
2000 LXI SP,2099H
2003 LXI H ,42F2H
2006 PUSH H
2007 DELAY COUNTER
200F
2010 POP H
Load the stack pointer
register with the
address 2099.
Loads data in the HL
register pair.
The content of the HL
register pair pushed
into stack.
Saved data in stack
pointer register to HL
register pair. 6SSP/EC-502/2020
7. PUSH Instruction
The stack pointer is
decremented by one to 2098 H ,
and the contents of the H
register are copied to memory
location 2098H.
The stack pointer register is
again decremented by one to
2097H,and the contents of the L
register are copied to memory
location 2097H.
The contents of the register pair
HL are not destroyed ; however
HL is made available for delay
counter.
8085 Register
A
B
D
H
S
p
F
C
E
L
Memory
2097
2098
2099
Contents on the stack &in the register
after the PUSH instruction
Sp
42 F2
2099
X
42
F2
7SSP/EC-502/2020
8. POP Instruction
The contents of the top of the
stack location shown by the
stack pointer are copied in the L
register and the stack pointer
register is incremented by one to
2098 H.
The contents of the top of the
stack (now it is 2098H) are
copied in the H register, and the
stack pointer is incremented by
one.
The contents of memory location
2097H and 2098 are not
destroyed until some other data
8085 Register
A
B
D
S
p
F
C
E
Memory
2097
2098
2099
H L
Contents on the stack and in the
registers after the POP instruction
Sp
2097
42 F2
X
42
F2
8SSP/EC-502/2020
9. 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 stack and then the pointer is
incremented.
The SP pointer always points to “the top of
the
9SSP/EC-502/2020
10. LIFO Function
The order of PUSHs and POPs must be
opposite of each other in order to
retrieve information back into its original
location.
PUSH H
PUSH L
...
POP L
POP H
10SSP/EC-502/2020
11. The PSW Register Pair
o The 8085 recognizes one additional register pair called
the PSW (Program Status Word).
o This register pair is made up of the Accumulator and the Flags
registers.
o It is possible to push the PSW onto the stack, do
whatever operations are needed, then POP it off of the
stack.
o The result is that the contents of the Accumulator and the
status of the Flags are returned to what they were before the
operations were executed.
11SSP/EC-502/2020
12. 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.
12 80
A Flag
FFFF
FFFE
FFFD
FFFC
FFFB
12
80
12SSP/EC-502/2020
13. 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
FFFF
FFFE
FFFD
FFFC
FFFB
80
12
A Flag
12 80
13SSP/EC-502/2020
14. Cautions with PUSH and POP
● PUSH and POP should be used in opposite
order.
● There has to be as many POP’s as there are
PUSH’s.
● If not, the RET statement will pick up the
wrong information from the top of the stack
and the program will fail.
● It is not advisable to place PUSH or POP
inside a loop.
14SSP/EC-502/2020
15. Subroutines
A subroutine is group of instruction
written separately from the main
program to perform a function that
occurs repeatedly in the main
program.
o Rather than repeat the same instructions
several times, they can be grouped into a subroutine
that is called from the different locations.
o In Assembly language, a subroutine can exist
anywhere in the code.
o However, it is customary to place subroutines
separately from the main program.
15SSP/EC-502/2020
16. Subroutines Cont…
When a main program calls a
subroutine the program execution is
transferred to the subroutine, after
the completion of the subroutine ,the
program execution returns to the
main program.
The microprocessor uses the stack
to store the return address of the
subroutine.
16SSP/EC-502/2020
17. Subroutines Cont…
The 8085 has two instructions for
dealing with subroutines.
– The CALL instruction is used to
redirect program execution to the
subroutine.
– The RET instruction is used to
return to the main program at the
end of the subroutine .
17SSP/EC-502/2020
18. The CALL instruction
CALL ,16 bit
Call subroutine in conditionally
located at the memory address
specified by the 16 bit operand.
This instruction places the address of
the next instruction on the stack and
transfer the program execution to the
subroutine address.
18SSP/EC-502/2020
19. The CALL instruction
● CALL 4000H
o 3-byte instruction.
o Push the address of the instruction immediately
following the CALL onto the stack and decrement the stack
pointer register by two.
o Load the program counter with the 16-bit address
supplied with the CALL instruction.
o Jump Unconditionally to memory location.
FFFF
FFFE
FFFD
FFFC
20 03
40 00
CALL 4000
[W] [Z] Register
PC
03
20
19SSP/EC-502/2020
400
0
20. The CALL instruction cont…
MP reads the subroutine address from the
next two memory location and store the
higher order 8 bit of the address in the W
register and stores the lower order 8 bit of
the address in the Z register.
Push the address of the instruction
immediately following the CALL onto the
stack [ Return address].
Load the program counter with the 16-bit
address supplied with the CALL
instruction from WZ register.
20SSP/EC-502/2020
21. The RTE Instruction
● RTE
o 1-byte instruction
o Retrieve the return address from the top of the
stack and increments stack pointer register by two.
o Load the program counter with the return
address.
o Unconditionally returns from a subroutine.
FFFE
FFFF
FFFD
FFFC
20
03
20PC
21SSP/EC-502/2020
032003
22. CALL and RET Function
Main prog.
◦ . F1 F2
◦ . . .
◦ CALL F1 . .
◦ RET 1 CALL F2 RET
2
◦ . RET 2
◦ . .
◦ . .
◦ RET 1 X
RET 1
RET 2
22SSP/EC-502/2020
Stack
24. Conditional CALL and RTE Instructions
The 8085 supports conditional CALL and
conditional RTE instructions.
– The same conditions used with conditional
JUMP instructions can be used.
– CC, call subroutine if Carry flag is set.
– CNC, call subroutine if Carry flag is not set
– RC, return from subroutine if Carry flag is
set
– RNC, return from subroutine if Carry flag
is not set.
24SSP/EC-502/2020
25. Passing Data to a Subroutine
Data is passed to a subroutine through
registers.
Call by Reference:
◦ The data is stored in one of the registers by the
calling program and the subroutine uses the
value from the register.The register values get
modified within the subroutine. Then these
modifications will be transferred back to the
calling program upon returning from a subroutine
Call by Value:
◦ The data is stored in one of the registers, but the
subroutine first PUSHES register values in the
stack and after using the registers, it POPS the
previous values of the registers from the stack
while exiting the subroutine. i.e. the original
values are restored before execution returns to
the calling program. SSP/EC-502/2020 25
26. Passing Data to a Subroutine
The other possibility is to use agreed
upon memory locations.
◦ The calling program stores the data in the
memory location and the subroutine
retrieves the data from the location and
uses it.
SSP/EC-502/2020 26
27. A Proper Subroutine
According to Software Engineering
practices, a proper subroutine:
Is only entered with a CALL and exited
with an RTE
Has a single entry point
◦ Do not use a CALL statement to jump into
different points of the same subroutine.
Has a single exit point
◦ There should be one return statement
from any subroutine
SSP/EC-502/2020 27