Texas Instruments MSC1210 manual Using the Stack PUSH/POP

Models: MSC1210

1 324
Download 324 pages 20.97 Kb
Page 222
Image 222

Using the Stack (PUSH/POP)

16.27 Using the Stack (PUSH/POP)

The stack, as with any processor, is an area of memory that can be used to store information temporarily, including the return address for returning from subroutines that are called by ACALL or LCALL. The 8052 automatically handles the stack when making an ACALL or LCALL, as well as when returning with the RET instruc- tion. The stack is also handled automatically when an ISR is triggered by an inter- rupt, and when returning from the ISR with the RETI instruction.

Additionally, the stack can be used for your purposes and for temporary stor- age by using the PUSH and POP instructions. The PUSH instruction puts a value onto the stack, and the POP instruction takes off the last value put on the stack. A value can be saved temporarily by PUSHing it onto the stack, and that value may be restored by POPing it.

Note:

The stack operates on a last in first out (LIFO) basis. When PUSHing the val- ues 4, 5, and 6 (in that order), POPing them one at a time will return 6, 5, and then 4. The value most recently added to the stack is the first value that will come off when executing a POP instruction.

An example using the PUSH and POP instructions is:

MOV A,#35h

;Load the accumulator with the value 35h

PUSH ACC

;Push accumulator onto stack, accumulator still holds 35h

ADD

A,#40h

;Add 40h

to the accumulator, accumulator now holds 75h

POP

ACC

;Pop

the

accumulator from stack, accumulator holds

 

 

;35h

again

The above code is functionally useless. However, it does illustrate how to use

PUSH and POP.

The code starts by assigning 35H to the accumulator. It then PUSHes it onto the stack. Then it adds 40H to the accumulator, just to change the accumulator to something else. At this point the accumulator holds 75H. Finally, it POPs from the stack into the accumulator. The POP restores the value of the accu- mulator to 35H because the last value pushed onto the stack was 35H.

Note:

When PUSHing or POPing the accumulator, it must be referred to as ACC because that is the memory location of the SFR. The instructions PUSH A and POP A cannot be assembled—both of these will result in an assemble- time error in most, if not all, 8052 assemblers.

When using PUSH, the SFR or internal RAM address that follows the PUSH instruction is the value that is PUSHed onto the stack. For example, PUSH ACC pushes the value of the accumulator onto the stack. PUSH 60h pushes the value of internal RAM address 60H onto the stack.

Likewise, the internal RAM address or SFR that follows a POP instruction indi- cates where the value should be stored when it is POPed from the stack. For ex- ample, POP ACC pops the next value off the stack and into the accumulator. POP 40h pops the next value off the stack and into internal RAM address 40H.

16-28

Page 222
Image 222
Texas Instruments MSC1210 manual Using the Stack PUSH/POP