stacks & subroutines
stacks & subroutines
The stack is a LIFO (last in, first out) data structure implemented in the RAM area
The stack is an area of memory identified by the programmer for temporary storage of information.
Used to store addresses and data when the microprocessor branches to a subroutine.
Apart from the general purpose registers A, B, C, D, E, H, and L, and the Flags registers, there are
They are the stack pointer, SP, and the program counter, PC.
The SP register will hold the address of the top location of the stack.
The PC will hold the address of the memory location from where the next instruction for execution is
to be fetched.
The stack normally grows backwards into memory.
In other words, the programmer defines the bottom of the stack and the stack grows up into
reducing address range.
There are two methods to add data to the stack: Direct method and
Indirect method
Direct method
In the direct method, the stack pointers address is loaded into the
stack pointer register directly.
In the indirect method, the stack pointers address is loaded into the
stack pointer register via another register pair.
LXI H, 8000H
SPHL
LXI H, 1234H
PUSH H
POP D
HLT
CALL Execution ● Instruction requires five machine cycles and eighteen T- states: Call
instruction is fetched, 16-bit address is read during M2 and M3 and stored temporarily in W/Z
registers. In next two cycles content of program counter are stored on the stack (address from
where microprocessor continue it execution of program after completion of the subroutine.)
RET Execution ● Program execution sequence is transferred to the memory location 2043H location.M1 is
normal fetch cycle during M2 contents of stack pointer are placed on address bus so 43H data is fetched and
stored on Z register and SP is upgraded. Similarly for M3. Program sequence is transfered to2043H by placing
contents of W/Z on address bus.
What are the advantages of using subroutines?
Write a program that will display FF and 11 repeatedly on the seven segment
display. Write a delay subroutine and call it as when necessary.
(a) Unconditional Call Instructions: It transfers the program sequence to the memory address
given in the operand.
(a) Unconditional Return Instruction: The program sequence is transferred unconditionally from the
subroutine to the calling program.