Reentrant Subroutine Example

;making a stack frame for local data

psha ;save registers

pshx

leas 5,-sp ;Create a 5 byte space

tsx ;sp to X

ldaa PORTA ;Example data

staa 0,x ;Store on stack

ldaa PORTB ;Example data

staa 1,x ;Store on stack

continued on next slide. . .

Notes:

This subroutine allocates 5 bytes within the stack frame for storage of local values. The stack pointer is decremented by five. Up to five bytes of local data can be saved and modified. The value in the stack pointer is copied into index register X to access the frame. If this subroutine is interrupted and called during the interrupt sequence, local storage space within a new stack frame will be allocated. The LEAS instruction (load stack pointer with effective address) increments the stack pointer to create storage space. The TSX instruction (transfer from stack pointer to index register X) loads the contents of the stack pointer into the X register. This becomes the frame pointer.The next slide shows the subroutine termination.