Home » Microprocessors and Microcontrollers » What is Stack in Assembly Language?

What is Stack in Assembly Language?

The Stack in Assembly Language is a group of memory locations in Read/Write (R/W) memory of any microcomputer and is used to store the contents of the register, operand and memory address. The starting location of the Stack in Assembly Language is defined by loading a 16-bit address into the stack pointer, a reserved space usually at the top of memory map. Theoretically, the size of the stack is unlimited, but it is restricted only by the available R/W memory in a microcomputer system. The stack can be initialized anywhere in the user-memory map, but the stack is initialized at the highest user-memory location so that there will not be any interface with the program.

In 8085 microprocessor systems, the beginning of the stack is defined in the program by using the instruction LXI SP, 16-bit. The LXI SP is a 16-bit state that loads the 16-bit address into the stack pointer register. Then contents of register pairs (BC, HL, etc.) can be stored in two consecutive stack memory loca­tions by using the instruction PUSH and can be retrieved from the stack into register pairs by using the POP instruction. The microprocessor keeps track of the next available stack memory location by incrementing or decrementing the address in the stack pointer. The address in the stack pointer (register) always points to the top of the stack and indicates that the next memory location (SP-1) is available to store information.

This method of information storage looks like the process of stacking books one above another. Therefore, data is always retrieved from the top of the stack. So data are stored in the stack on Last-In-First-Out (LIFO) principle. The syntax of stack instructions to store data on and retrieve data from the stack are given below:

Stack in Assembly Language

Figure 4.3 shows a stack and stack top location. The SP register holds the address of the stack top location, i e, 8004 H.

Stack in Assembly Language

For example, a program is stored in memory locations starting from 7000H as given below, and the stack is initialized at the location 8004H.

The position of a stack before PUSH operation is depicted in Fig. 4.3. When the program is executed, the contents of the register pair HL must be pushed to the stack. After the PUSH operation, the stack position is changed to 8001H. In the same way, a POP operation is used to transfer the contents from the stack to the register.

Stack in Assembly Language

The stack position before and after the PUSH operation has been given in Fig. 4.4 (a) and Fig. 4.4 (b) respectively. Figures 4.5 (a) and 4.5 (b) show the stack position before and after the POP operation correspondingly.

Stack in Assembly Language

From the above example, the following points can be summarized:

  1. During the execution of a program, a 16-bit address (8004H) is stored in the stack pointer register. The stack space grows upward in the numerically decreasing order of memory addresses. The contents of HL register pairs can be stored beginning from the next location (SP-1).
  2. The PUSH instructions are used to store contents of register pairs on the stack, and the POP instruc­tions are used to retrieve the information from the stack. The address in the stack pointer register always points to the top of the stack, and the address is decremented as information is stored or retrieved, respectively.
  3. The storage and retrieval of the content of registers on the stack should follow the LIFO (Last-In-­First-Out) sequence.
  4. Information in the stack locations may not be destroyed until new information is stored in that mem­ory location.