The Stack and Stack Pointer in 8051 Microcontroller

In this article, we will discuss about the concept of stack and stack pointer in 8051 microcontroller.

8051 Stack Pointer

  • The stack is a section of internal RAM used by the CPU to store information temporarily. This information could be data or an address.
  • The CPU needs this storage area since there are only limited number of registers. The register to access the stack is called Stack Pointer.
  • The stack pointer (SP) is an 8-bit register within the SFR area, with the address 81H and it can point an address location between 00h to FFh.
  • Stack pointer is used to hold the address of Top of the Stack. The address held in the SP is the location in internal RAM where the last byte of data was stored by a stack operation.
  • This register can hold one 8-bit address at a time, which is actually the memory location at top of the stack.
  • The stack pointer is initialized to the value 07H when the 8051 microcontroller is reset.

Stack Operation

  • When data is to be placed on the stack, the SP increments one before storing the data on the stack. When the data is retrieved from the stack, the SP decrements one after retrieving the data.

Detailed stack operation shown in figure.

The Stack and Stack Pointer in 8051 Microcontroller
  • In the 8051, the stack is configured as a series of memory locations following the Last-In First-Out (LIFO) pattern.
  • In general, the stack is initialized in the internal RAM area. Any 8-bit data can be stored and retrieved from the stack using PUSH and POP instructions, with the help of the stack pointer.
  • A push operation in the 8051 is used to store an 8-bit data in the stack.
  • The PUSH instruction first increments the value of SP and then stores the data mentioned in the instruction in the memory location pointed to by SP.
  • Similarly, the POP instruction stores the value from the top of the stack in the register mentioned in the instruction and then decrements the value of SP.
  • The other instructions of the 8051 that affect the stack and the stack pointer are ACALL, LCALL, RET, and RETI.
  • The stack pointer can be initialized to any internal RAM address by the programmer, by writing the required address in the SP SFR address 81H.

Recent posts

Leave a Comment

Your email address will not be published. Required fields are marked *