Stack
Stack
PUSH
POP
SUBROUTINE
CALL
RET
STACK
PUSH Rp: This is a 1-byte instruction. It copies the contents of the specified register pair on the
stack by decrementing stack pointer register, and the contents of the high-order register are copied
in the location shown by the stack pointer register. The stack pointer register is again decremented
and the contents of the low-order register are copied in that location. The operands B, D, and H
represent register pairs BC, DE, and HL, respectively. The operand PSW represents Program Status
Word, meaning the contents of the accumulator and the flags.
POP Rp: This is a 1-byte instruction. It copies the contents of the top two memory locations of the
stack into the specified register pair. First, the contents of the memory location indicated by the
stack pointer register are copied into low-order register and then the stack pointer register is
incremented by 1. The contents of the next memory locations are copied into the high-order
register and the stack pointer register is again incremented by 1.
In this example the first instruction LXI SP 2099H loads the stack pointer register with the address
2099H. This instruction indicates to the microprocessor that memory space is reserved in the R/W
memory as the stack and that the locations beginning at 2098H and moving upward can be used
for temporary storage. This instruction also suggests that the stack can be initialized anywhere in
the memory, however the stack location should not interfere with a program. The next instruction
LXI H loads data in the HL register pair.
When instruction PUSH H is executed the following sequence of data transfer takes place. After the
execution, the contents of the stack and the register are as shown in figure .
1. The stack pointer is decremented by one to 2098H, and the contents of the H register are copied to
memory location 2098H.
2. The stack pointer register is again decremented by one to 2097H, and the contents of the L register are
copied to memory location 2097H.
3. The contents of the register pair HL are not destroyed; however, HL is made available for the delay
counter.
Contents on the Stack and in the Registers after the PUSH Instruction.
1. 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 2098H.
2. 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.
3. The contents of memory locations 2097H and 2098H are not destroyed until some other data bytes
are stored in these locations.
Contents on the Stack and in the Registers after the POP Instruction.
Summary
SUBROUTINE
Subroutine Call and Program Transfer
CALL INSTRUCTION
i. ‘JUMP’ is used generally to break out within the same program module(like C++)
ii. To jump from within the module to outside modules, we use “call instruction”
iii. CALL is a 3-byte instruction that transfers the program sequence to a 16-bit subroutine address specified
in the operand. It saves the contents of the program counter (the address of the next instruction) on the
stack. Its decrements the stack pointer register by 2 and jumps unconditionally to the memory location
specified by the second and the third bytes. This instruction is accompanied by a return instruction in the
subroutine.
iv. The conditional call instructions are:
CC (Call if CY=1)
CNC (Call if CY=0)
CM (Call if S=1; negative number)
CP (Call if S=0; positive number)
CPO (Call if P=0; odd parity)
CPE (Call if P=1; even parity)
CZ (Call if Z=1)
CNZ (Call if Z=0)
RET INSTRUCTION
i. We must return back from outside the module to the main module by using return instruction.
ii. Even return instruction can be with or without condition:
RET (unconditional)
RC (return if CY=1)
RNC (return if CY=0)
RM (return if S=1; negative number)
RP (return if S=0; positive number)
RPO (return if P=0; odd parity)
RPE (return if P=1; even parity)
RZ (return if Z=1)
RNZ (return if Z=0).
All these return instructions are 1-byte instructions. Inserts the two bytes from the top of the stack
into the program counter and increments the stack pointer register by two.