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

150                                Chapter 6 Assembly Language Subroutines


            Before we begin, however, we reiterate that these techniques are quite similar to
        those used in Section 6.1 to store local variables. However, these techniques are used
        between subroutines, while the latter were used entirely within a subroutine.




         * SUBROUTINE DOT PRODUCT
         DOTPRD:   MUL                    ; First term is now in D
                   EXG      D, Y          ; Store first term in Y, get W(2) in B
                   EXG      A,X           ;V(2)intoA
                   MUL                    ; Calculate second term
                   LEAY     D, Y          ; Add terms, to get result in Y
                   RTS                    ; Return to the calling program

                                      a. A subroutine

                   LD AA    # 2           ; Copy of V( 1) into A
                   LDX      # 7           ; Copy of V(2) into low byte of X
                   LDAB     # 6           ; Copy of W( 1) into B
                   LDY      # 3           ; Copy of W(2) into low byte of Y
                   BSR      DOTPRD        ; Call the subroutine
                   STY      DTPD          ; Store dot product in DTPD


                                    b. A calling sequence
                   LDAA     LV, SP        ; Copy of V(l) into A
                   LDX      LW-1, S P     ; Copy of V(2) into low byte of X
                   LDAB     LV+1,SP       ; Copy of W(l) into B
                   LDY      LW, S P       ; Copy of W(2) into low byte of Y
                   BSR      DOTPRD        ; Call the subroutine
                   STY      DTPD          ; Store dot product in DTPD

                                 c. Another calling sequence

                      Figure 6.15. A Subroutine with Parameters in Registers




            In this section we examine six methods used to pass parameters to a subroutine. We
         illustrate each method with the dot product from Section 6.1. We first consider the
         simplest method, which is to pass parameters in registers as we did in our earlier
        examples. Then the passing of parameters by global variables is discussed and
        discouraged. We then consider passing parameters on the stack and after the call, which
        are the most common methods used by high-level languages. We then discuss the
        technique of passing parameters using a table, which is widely used in operating system
         subroutines.
   168   169   170   171   172   173   174   175   176   177   178