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

148                                Chapter 6 Assembly Language Subroutines

         was used for the local variables of the outer program segment. It is always in a known
         position on the stack (in this case, on the very top of the stack), so it is easy to find. See
         Figure 6.14, where the inner program segment can access the local variables of the outer
         segment by loading the stack marker into any index register and using index addressing to
         get the variable. Note that the stack marker is deallocated together with the other stacked
         local variables at the end of the program segment.

        MARK A: EQU 0                  ; Stack mark for segment A
        W:        EQU    2             ; Input vector
        WW:       EQU    4             ; Input vector
        SlZEAi    EQU 6
         *
        START A: TFR SP,X              ; Start for segment A
                  LEAS -SIZEA,SP
                  STX MARKA,SP
                  MOVW #$102,W,SP ; Initialize both bytes of VV
                  MOW #$304,WW,SP ; Initialize both by tes of WW
         *
        MARKB : EQU 0                  ; Stack mark for segment B
        TERM:     EQU    2
        SIZEB:    EQU    4
         *
        STARTS: TFR SP,X
                  LEAS -SIZEB,SP
                  STX MARKB,SP
                  LDAA VV,X            ;V(l)intoA
                  LDAB WW,X            ;W(l)intoB
                  MUL                  ; First term is now in D
                  STD    TERM, SP      ; Store first term in TERM
                  LDAA W+1,X           ;V(2)intoA
                  LDAB WW+1,X          ;W(2)intoB
                  MUL                  ; Calculate second term
                  ADDD TERM,SP         ; Add in TERM; dot product in D
        ENDB:     LEAS SIZEB ,SP       ; End of segment B
        *
        ENDA:     LEAS SIZEA,SP        ; End of segment A
                Figure 6.14. Accessing Stacked Local Variables Using a Stack Marker

             Either the extended local access or the stack marker access mechanisms can be used
         in cases where program segments are further nested. Consider program segment C, with
         SIZEC stacked local variables, which is nested in segment B and needs to load
         accumulator A with the value of SA, a stacked local variable of segment A. Using
         extended local access, as in the first example, the following instruction will accomplish
         the access.

                        LDAA    SIZEC+SIZEB+SA,SP
   166   167   168   169   170   171   172   173   174   175   176