Thursday, March 17, 2016

8051 Register Bank and Stack

The 8051 has a total of 128 bytes of RAM. These 128 bytes are divided into three different groups as follows.
  • 00H – 1FH (32 bytes): Register Bank and Stack
  • 20H – 2FH (16 bytes): Bit addressable read/write memory
  • 30H – 7FH (80 bytes): Scratch Pad - read/write storage
Register Bank: There are 4 register banks with 8 registers (R0 – R7) each. Each register is 8-bit. Register bank 0 is the default when the 8051 is powered up. We can switch to other banks by use of the PSW (program status word) register. Bits D4 and D3 of the PSW are used to select the desired register bank.

Stack: The stack is a section of 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 a limited number of registers. The register used to access the stack is called the SP (stack pointer) register. The stack pointer is 8 bits wide and it can take values of 00 to FFH. When the 8051 is powered up, the SP register contains value 07. This means that RAM location 08 is the first location used for the stack by the 8051. The storing of a CPU register in the stack is called a PUSH, and pulling the contents off the stack back into a CPU register is called a POP.
For example,
MOV R1, #25H
MOV R4, #45H

  • the instruction “PUSH 1″ increments SP to 08H  and pushes register R1 onto the stack location 08H.
  • the “PUSH 4″ increments SP to 09H and pushes register R4 onto the stack location 09H.
  • the instruction “POP 3″ copies the top of stack (location 09H) to register R3 and decrements SP to 08H. 
  • the “POP 6″ copies the top of stack (location 08H) to register R6 and decrements SP to 07H.
Locations 08 to IF can be used for the stack, in this case the upper limit of stack is 1FH. If in a given program we need more than 24 bytes (08 to 1FH = 24 bytes) of stack, we can change the SP to point to RAM locations 30 – 7FH. This is done with the instruction “MOV SP, #xxH”. Since SP = 07 when the 8051 is powered up, the first location of the stack is RAM location 08, which also belongs to register RO of register bank 1. In other words, register bank 1 and the stack are using the same memory space. If in a given program we need to use register banks 1 and 2, we can reallocate another section of RAM to the stack.

Related topics:
8051 Flags Bits   |   8051 Program Flow   |   8051 Timers   |   8051 Serial Port   |   8051 Interrupts

List of topics: 8051

No comments:

Post a Comment