Page 97 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 97

74                                             Chapter 3 Addressing Modes



















                     Figure 3.14. Nonreentrant Subroutine to Clear Memory

            This program fails to work correctly if it is interrupted after the STAA instruction is
        executed, and before the RTS instruction is executed and the interrupt handler calls this
        subroutine. The second call to this subroutine will wipe out the counter at location $822
        because the second call will also use this same location and will leave it cleared when the
        first call to this subroutine resumes execution. The first call to this subroutine will not
        work the same way if the interrupt occurs as it does if the interrupt doesn't occur.
        However, the subroutine in Figure 3.15 will work correctly if it is similarly interrupted.
            The key idea behind both recursion and reentrancy is to keep data on the stack. The
        stack provides new storage locations for each instantiation of the subroutine to keep its
        variables separate from the variables of other instantiations, so they aren't destroyed,
            Note that the decrement instruction accesses the counter on the stack without
        pulling the byte. If three 1-byte items were pushed on the stack, the instruction LDAA
        2, SP will read into accumulator A the first byte pushed without removing it from the
        stack. In general, items can be pushed on the stack at the beginning of a procedure and
        pulled off at the end of the procedure. Within the procedure the items can be read and
        written using offsets from the stack pointer.





















                       Figure 3.15. Reentrant Subroutine to Clear Memory
   92   93   94   95   96   97   98   99   100   101   102