The PUSH and POP opcodes specify the direct address of the data. The data moves between an area of internal RAM, known as the stack, and the specified direct address. The stack pointer special-function register (SP) contains the address in RAM where data from the source address will be PUSHed, or where data to be POPed to the destination address is found. The SP register actually is used in the indirect addressing mode but is not named in the mnemonic. It is implied that the SP holds the indirect address whenever PUSHing or POPing. Figure 3.3 shows the operation of the stack pointer as data is PUSHed or POPed to the stack area in internal RAM.
A PUSH opcode copies data from the source address to the stack. SP is incremented by one before the data is copied to the internal RAM location contained in SP so that the data is stored from low addresses to high addresses in the internal RAM. The stack grows up in memory as it is PUSHed. Excessive PUSHing can make the stack exceed 7Fb (the top of internal RAM). after which point data is lost.
A POP opcode copies data from the stack to the destination address. SP is decremented by one after data is copied from the stack RAM address to the direct destination to ensure that data placed on the stack is retrieved in the same order as it was stored.
The PUSH and POP opcodes behave as explained in the following table:
Mnemonic | Operation |
PUSH add | Increment SP; copy the data in add to the internal RAM address contained in SP |
POP add | Copy the data from the internal RAM address contained in SP to add; decrement the sp |
The SP register is set to 07h when the 8051 is reset, which is the same direct address in internal RAM as register R7 in bank 0. The first PUSH opcode would write data to R0 of bank 1. The SP should be initialized by the programmer to point to an internal RAM address above the highest address likely to be used by the program.
The following table shows examples of PUSH and POP opcodes:
Mnemonic | Operation |
MOV 81h,#30h | Copy the immediate data 30h to the SP |
MOV R0, #0ACh | Copy the immediate data ACh to R0 |
PUSH 00h | SP = 31h; address 31h contains the number ACh |
PUSH 00h | SP = 32h; address 32h contains the number ACh |
POP 0lh | SP = 31 h; register R 1 now contains the number Ach |
POP 80h | SP = 30h; port 0 latch now contains the number ACh |
When the SP reaches FFh it "rolls over" to 00h (R0).
RAM ends at address 7Fh; PUSHes above 7Fh result in errors.
The SP is usually set at addresses above the register banks.
The SP may be PUSHed and POPed to the stack.
Note that direct addresses, not register names, must be used for most registers. The stack mnemonics have no way of knowing which bank is in use.
Labels: Moving Data on 8051