Page 123 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 123
100 Chapter 4 Assembly Language Programming
ORG $800
K: DC . b "ALPHA", 0 ; a NULL-terminated character string for part (b).
OUTPUT: Ds. b 10 ; storage buffer for output characters for part (c).
OUTPTR: DC . w OUTPUT ; pointer to the above buffer
a. Data
PRINT: LDX #K ; get address of string
NEXT: LDAA 1, X+ ; get a character of string, move pointer
BEQ END ; if it is NULL, exit
BSR PUT ; otherwise print the character in A
BRA NEXT ; repeat the loop
END: SWI ; return to the debugger
b. Calling PUT
PUT: PSHX ; save
LDX OUTPTR ; get pointer to output string
STAA 1, X+ ; save character, move pointer
STX OUTPTR ; save pointer
RTS ; return
PULX ; restore
Figure 4.8. Print Program
programming effort. From now on, we will not write machine code, but we will write
(ASCII) source code and use the assembler to generate the machine code.
The first three examples illustrate character string processing. The first example
prints out a character string. The second transfers a character string from one location to
another. The third compares two strings, returning 1 if they match. These examples are
similar to PUT, STRCPY, and STRCMP subroutines used in C.
Figure 4.8b's program prints a string of characters using a subroutine PUT, like
problem 3.15. Such strings often end in a NULL (0) character. The program reads
characters from the string using LDAA 1, X+, and calls PUT to print the character in A.
This also sets the condition code Z bit if the byte that was loaded was NULL, which
terminates execution of the loop. An analogous program inputs data from a keyboard
using the subroutine GET and fills a vector with the received characters until a carriage
return is received. These programs can be generalized. Any subroutine that uses characters
from a null-terminated character string can be used in place of PUT, and any subroutine
that puts characters into a string can be used instead of GET.
PUT and GET are actually I/O procedures we show in §11.8, which require
considerable understanding of I/O hardware. We don't want to pursue the actual PUT and
GET subroutines quite yet. Instead, we replace the actual PUT and GET subroutines with
a stub subroutine (Figure 4.8c). After stopping the computer, examine the string
OUTPUT to see what would be output. Similarly, a stub subroutine can be used instead
of GET, to "input" characters. The sequence of input characters is preloaded into a string.
Our second example (Figure 4.9) copies a null-terminated character string from one
location to another. The original string is generated by the assembler and downloaded
into memory, using Src DC . b. The program copies it to another part of memory at
Dst Ds . b. Note that the NULL is also copied to the destination string.