Page 322 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 322
10.3 Sequential Data Structures 299
In assembly language, a string's length can be ascertained in one of three ways: by also
giving its length, either by knowing it implicitly or giving it in a variable, by
terminating it with a special character such as null, carriage return, or $4 (end-of-text), or
by setting the sign bit in the last, and only the last, character in the string. In C, a
pointer is generally used to access the current location of a string. For instance, suppose
we declare global char *ptr; and later we initialize ptr = s;. Then the pointer ptr
can be used to access the current location. Alternatively, a numerical index can be used,
declared as in char i;, and the index i can be intialized to zero, as in i = 0;, so that
s [ i ] is the first character in the string s. In assembly language, this string (9) can
appear in the program area as a constant to be displayed on a terminal or to be printed on
a printer. It is often in the program area because it may be stored in ROM together with
the program. To preserve position independence, the address of the string is generally put
in an index register using program counter relative addressing as in
LEAK S,PCR
With this instruction, the address of the first character of s will be put into X regardless
of where the ROM containing the program is placed in memory. A numerical index can
also be used to read elements of a string in assembly language, as we did in C (see the
problems at the end of the chapter).
Strings can be accessed at a current location, which can be moved as the string is
accessed. In C, we can access this element and move to the next location using *ptr++
or *ptr~~. Alternatively, if we use an index to access the elements, we can increment
or decrement an index, as in the expression s [ i++ ] or s [ i— ]; In assembly language,
if the current location is a memory word whose address is in the index register X, the
string can be accessed by instructions like LDAA 0, X, LDAA 1, X+, or LDAA 1, X.
In C, operations on a string can repeat until the null character is read at the end of
the string, as in the statement while (*ptr) f ( *ptr++);. If we are using indexes to
access characters, this operation can be written as while (s [ i ]) f (s [ i++ 3) *,. In
assembly language, if a null-terminated string is used, the load instruction such as LDAA
1 f X+ will be followed by a branch instruction such as BEQ END, to terminate when
the null character is read. If another special character such as a carriage return is used,
then a CMPA instruction can be used to detect the end of the string. If a sign bit indicates
the end of the string, then the LDAA 1, X+ will be followed by a branch instruction
such as BMI END, to terminate when the last character is read. The program generally
has to strip off the sign bit before using the last character.
Strings of characters are frequently input and output from a terminal using a buffer.
To discuss this, we first need to make some remarks about how single characters are
input and output between a terminal and the MPU. In C, there is generally a procedure
such as is given by the prototype char inch(); (for "input character") to input
characters from the keyboard. It waits for a key to be depressed at the terminal; and,
when the key is depressed, the procedure returns to the calling routine with the ASCII
code of the key depressed. There is also generally a procedure such as is given by the
prototype void outch(char c); (for "output character") to output characters to the
screen; it generally displays the ASCII contents of the seven low-order bits of c, but
control characters, such as carriage return ($OD) and line feed ($OA), will move the screen
cursor in the usual way. (The remaining ASCII characters are used for different purposes