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

6.1 Local Variables                                                 141


















             Figure 63. Changing a Global Variable before It Has Been Completely Used



        illustrates a program segment using TEMP to store a variable to be recalled later. Before
        that value is recalled, however, TEMP has been changed by subroutine B, which is called
        by subroutine A, which itself is called by the program segment. This case is difficult to
        debug because each subroutine will work correctly when tested individually but will not
        work when one is called, either directly or indirectly through other subroutines, from
        within the other. This technique also confuses documentation, specifically the meaning
        of the local variable TEMP, generally making the program less clear.
            With the other technique, the local variables will be put in different memory
        locations, having different symbolic names. See Figure 6.5. This approach is superior to
        the last approach, because differently named local variables, stored in different locations,
        will not interfere with the data stored in other locations. The names can be chosen to
        denote their meaning, reducing the need for comments. However, memory is taken up by
        these local variables of various program segments, even though they are hardly ever used.
        In a single-chip 'A4 or 'B32, only IK bytes of SRAM are available. Using all these
        bytes for rarely used local variables leaves less room for the program's truly global data.


          TEMP: D S     6           ; Allocate 6 bytes of memory for temporary variables
          enter: MOVB#1, TEMP       ; Allocate and initialize V( 1)
                 MOVE #2,TEMP+1 ; Allocate and initialize V(2)
                 MOVE #3,TEMP+2 ; Allocate and initialize W(l)
                 MOVE #4,TEMP+3 ; Allocate and initialize W(2)
                 LDAA TEMP          ; V(l) into A
                 LDAB TEMP+2        ; W(l) into B
                 MUL                ; The value of first term is now in D
                  STD   TEMP+4      ; Store first term in TERM
                 LDAA TEMP+1        ; V(2) into A
                 LDAB TEMP+3        ; W(2) into B
                 MUL                ; Calculate second term
                 ADDD TEMP+4        ; Add in TERM; dot product is now in D

         Figure 6.4. Inner Product Utilizing a Global Variable such as TEMP (a Bad Example)
   159   160   161   162   163   164   165   166   167   168   169