Page 124 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 124
4,4 Character String Operations 101
Src: DC . b "ALPHA", 0 ; initialization of the source string (downloaded)
Dst: Ds.bl6 ; allocation of the destination string
COPY: LDX #Src ; get address of source string
LDY #Dst ; get address of destination string
NEXT: LDAA 1, X+ ; get a character of string, move pointer
STAA I, Y+ ; store it in the destination
BNE NEXT ; if it is not NULL, reexecute the loop
Figure 4.9. Character Move Program Segment and Data
Our third example (Figure 4.10) compares one null-terminated character string with
another. Both strings are downloaded into memory starting at label Src and Cmprd. We
examine several cases of execution right after this program listing.
We examine several cases with this comparison program. Consider the case where
Src is "BETA." The first time after label NEXT, the CMPA instruction clears the Z
condition code bit, and the program goes to BAD to clear A and exit. Consider the case
where Src is "ALPH." The fifth time at label NEXT, the LDAA instruction sets the Z
condition code bit, and the program goes to EXIT where it tests the byte pointed to by
Y, which is the ASCII letter A, so it goes to BAD to again clear A and exit. Consider the
case where Src is "ALPHAS ." The sixth time after label NEXT, the CMPA instruction
clears the Z condition code bit, and the program goes to BAD to clear A and exit. Finally,
consider the case where Src is "ALPHA. " The fifth time at label NEXT, the LDAA
instruction sets the Z condition code bit, and the program goes to EXIT where it tests
the byte pointed to by Y, and because it is zero, the program sets A to 1 and exits. If the
two strings are identical, the program ends with 1 in A, otherwise it ends with 0 in A.
The next example illustrates a comparison of a letter provided in accumulator A, to
find a match among a collection of four letters L, A, S, and D, assuming it may not be
any other letter. The number left in B is 0 if the letter is L, 1 if it is A, 2 if S and 3 if
D. Two ways to do this are (1) execute compares with immediate operands, and (2) store
the letters in a string and compare the unknown letter to each letter in the string. See
Figure 4.11. Except for small collections of letters, the string method is best.
Src: DC . b "ALPHA", 0 ; source string (downloaded)
Cmprd: DC . b "ALPHA", 0 ; comparand string (downloaded)
CMPR: LDX #Src ; get address of source string
LDY #Cmprd ; get address of comparand string
NEXT: LDAA 1, X+ ; get a character of source string, move pointer
BEQ EXIT ; if it is NULL, exit the loop
CMPA 1, Y+ ; compare it to comparand character, move pointer
BEQ NEXT ; if it is the same, reexecute the loop
BAD: CLRA ; otherwise exit; A is cleared to indicate mismatch
SWI ; return to the debugger
EXIT: TST 0, Y ; see if compare character is also NULL
BNE BAD ; if it is not NULL, terminate indicating failure
LDAA #1 ; it must be identical - end with A set to 1
Figure 4.10. Character String Compare Program Segment and Data