Page 169 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 169
146 Chapter 6 Assembly Language Subroutines
TERM: EQU 0
VI: EQU TERM+2
V2: EQU Vl + 1
Wl: EQU V2 + 1
W2: EQU Wl + 1
MBYTES: EQU W2 + 1
Figure 6.11. Defining Symbolic Names for Stacked Local Variables by Sizes
Another technique, shown in Figure 6.12, uses the DS directive to play a trick on
the assembler. The technique uses the DS directive to bind the stacked local variables
partially with the stack pointer SP, using the location counter and the ORG directive to
modify the location counter. Recall that ALPHA DS 2 will normally allocate two bytes
for the variable ALPHA. The location counter is used to bind addresses to labels like
ALPHA as the assembler generates machine code. The location counter and hence the
address bound to the label ALPHA correspond to the memory location where the word
associated with the label ALPHA is to be put. A DS statement, with a label, binds the
current value of the location counter to the label (as the name of the container, not the
contents) and adds the number in the DS statement to the location counter. This will
bind a higher address to the next label, allocating the desired number of words to the label
of the current directive. Note that ALPHA EQU * will bind the current location counter
to the label ALPHA but not affect the location counter. Also, recall that the ORG
directive can set the location counter to any value. These can be used as shown in Figure
6.12.
You can reset the location counter to zero many times, and you should do this
before each group of DS directives that are used to define local storage for each program
segment. These DS statements should appear first in your program segment. Each set
should be preceded by a directive such as LCSAVE DS 0 to save the location counter
using LCSAVE and an ORG 0 directive to set the location counter to 0; and each set
should be followed by a directive such as ORG LCSAVE to set the origin back to the
saved value to begin generating machine code for your program segment. The last
directive in Figure 6.12, ORG LCSAVE, can be replaced by DS LCSAVE-*, which
avoids the use of the ORG statement. The DS directive adds its operand LCSAVE-* to
the location counter, so this directive loads LCSAVE into the location counter.
LCSAVE : EQU * ; Save current location counter
ORG 0 ; Set the location counter to zero
TERM: DS 2 ; First term of dot product
VI: DS 1 ; Copy of input vector element V(l)
V2 : DS 1 ; Copy of input vector element V(2)
W1: D S 1 ; Copy of input vector element W( 1)
W2: D S 1 ; Copy of input vector element W(2)
NBYTES:EQU* ; Number of bytes of local variables
ORG LCSAVE ; Restore location counter
Figure 6.12. Declaring Symbolic Names for Local Variables Using DS Directives