You are on page 1of 2

The Stack and Subroutines

The Stack Pointer (SP) must be initialized (or the stack must be defined) before any use of the stack.
All Memory Location below the stack pointer initializing address are then available for use as stack.
Any instruction that write data onto the stack automatically causes the SP address to decrement (8085)
If all the RWM has to be used as an stack, then the stack pointer must be initialized with the highest memory
address.
More than one stack can operate in a system by initializing stack pointer with a new address but then it will be
necessary to keep a track which stack was used for a
particular memory reference.
PUSH and POP instructions have STATUS registers
unaffected.
All stack instruction in the 8085A involves the transfer of
TWO bytes of data. Thus with this implementation, the TOP
of the stack actually consists of TWO registers.

Example:
- Initialize STACK Pointer (Load Register Pair Immediate)
LXI SP, data16

When designers wishes to set the Stack Pointer to a Value that has
been computed by the program, this value is placed in H and L and
then moved to the stack pointer, using the move HL to SP
instruction.

SPHL
(SP) <- (H)(L) Flags: None

Push rp ((SP) – 1) <- (rh)


Question: Exchange the content of HL and BC using Stack
((SP) – 2) <- (rl)
PUSH H
(SP) <- (SP) – 2
PUSH B
POP H Pop rp (rl) <- ((SP))
POP B (rh) <- ((SP) +1)
(SP) <- (SP) + 2
Processor Status Word:

PUSH PSW POP PSW


((SP) - 1) <- (A) (F) <- ((SP))
((SP) - 2) <- (F) (A) <- ((SP) +1)
(SP) <- (SP) – 2 (SP) <- (SP) + 2

Register Pair SP can’t be specified in a PUSH

Subroutines:
A subroutine provides more flexibility. The subroutine CALL instruction doesn’t replace the subroutine name by the set
of instructions in the subroutine. In this sense it is different from MACRO instructions.

A subroutine is executed by a CALL statement. The last statement in a subroutine is a RETURN (RET) statement which
returns the program control to the point immediately after the CALL statement.

Subroutines are implemented using stack. A CALL pushes the PC (Program Counter) content onto the STACK and then
the address contained in the CALL statement is loaded in to the PC causing a jump to the subroutine.

CALL address
RET
((SP) – 1) <- (PCH) (PCL) <- ((SP))
((SP) – 2) <- (PCL) (PCH) <- ((SP) + 1)
(SP) <- (SP) -2 (SP) <- (SP) +2
(PC) <- ((Byte 3)(Byte 2))

*****

You might also like