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

152                                Chapter 6 Assembly Language Subroutines




















              Figure 6.16. Change a Global Parameter before Its Subroutine Has Used It

         *          SUBROUTINE DOTPD - LOCAL VARIABLES
         TERM:     EQU      0             ; First term of the dot product
         MBYTES:   EQU      2
         *
         DOTPRD:   LEAS     -MBYTES, SP ; Allocate local variables
                   LDAA     V1            ; First component of V into A
                   LDAB     W1            ; First component of W into B
                   MUL                    ; First term of dot product into D
                   STD      TERM, SP      ; Save first term
                   LDAA     V2            ; Second component of V into A
                   LDAB     W2            ; Second component of W into B
                   MUL                    ; Second term of dot product into D
                   ADDD 2,SP+             ; Dot product into D, Deallocate loc var
                   STD      DTPD          ; Place dot product
                   RTS

                  Figure 6.17. A Subroutine with Parameters in Global Variables

            In assembly language, global variables are defined through a DS directive that is
        usually written at the beginning of the program. These variables are often stored on page
        zero on smaller microcontrollers so that direct page addressing may be used to access
        them. However in the 6812, page zero is used for I/O ports. Assuming that the directives
        are written somewhere in the program, the subroutine in Figure 6.17 does the previous
        calculation, passing the parameters through these locations. Note that we use local
        variables in this subroutine, as discussed in Section 6.1.
            The subroutine in Figure 6.17 uses global variables VI, V2, Wl, W2, and DTPD to
        pass parameters to the subroutine and from it. If the calling routine wants to compute the
        dot product of its local variables LV and LW, which each store a pair of 2-element 1-byte
        vectors, putting the result in LDP, the calling sequence in Figure 6.18 could be used.
        Notice that the calling routine's local variables are copied into global variables VI, V2,
        Wl, and W2 before execution and copied out of the global variable DTPD after execution.
        Any other calling sequence for this version of DOTPRD must also copy the vectors of
   170   171   172   173   174   175   176   177   178   179   180