Page 282 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 282
9.2 Expressions and Assignment Statements 259
If a statement gets a char variable and writes an int or unsigned int variable,
the result is sign extended when it is read. Figure 9.2's statement gsi = gsi + Isc;
or equivalently gsi += isc ,• is simply encoded as
LDAA 2, SP ; get 8-bit global variable Isc
SEX A, D ; upcast from char to int or unsigned int
ADDD $ 0 8 01 ; add in 16-bit global variable gsi
STD $ 0 801 ; put into 16-bit global variable gsi
But if a statement gets an unsigned char variable and writes an int or unsigned
int variable, the high byte is cleared before the unsigned char variable is read.
Figure 9.2's statement lui = guc - 17; is encoded as
LDAB $ 0 8 0 0 ; get 8-bit global variable guc saved earlier
CLRA ; upcast from unsigned char to int or unsigned int
SUED #17 ; subtract 17
STD 0, SP ; put into 16-bit global variable lui
You should observe that the declaration char or int affects the instruction data length,
and char and unsigned char determine whether, on upcasting, the 8-bit data is sign
extended with an SEX instruction or filled with zeros using a CLRA instruction.
The previous examples should indicate to the C programmer how to decide how a
variable is to be type cast. If its range of values is 0 to 127, declare it to be an
unsigned char, because upcasting is done with a short CLRA instruction rather than a
longer SEX instruction. If its range is 0 to 256, declare it to be an unsigned char,
but if the range is -128 to 127, declare it a char, to save space and time. Otherwise
declare it to be int.
To discuss how common operators are handled, we use the following main as an
example; it merely ANDs, ORs, multiplies, and divides some variables. Figure 9.3a's
program is compiled into the assembly-language program in Figure 9.3b.
Logical bit-by-bit ANDing is illustrated in Figure 9.3 by the expression Isc =
lsc& guc; or equivalently by Isc &= guc;, which is realized by
LDAA 2, SP ; get local variable Isc
ANDA $ 0 8 0 0 ; AND with global variable guc
STAA 2, SP ; put into local variable Isc
However, if one of the operands is constant, the BCLR instruction can be used. The
expression in Figure 9.3, Isc = lsc& 0x12;,or equivalently Isc &= 0x12; is
realized by
BCLR 2, SP, #2 3 7 ; AND local variable Isc with inverted constant 0x12
Note that the complement of the constant is used in the operand of BCLR. Logical bit-
by-bit ORing is illustrated in Figure 9.3 by the expression gsi - gsi | lui; or
equivalently by gsi | = lui;, which is realized by
LDD $ 0 8 01 ; get global variable gsi
ORAA 1, SP ; OR with high byte of local variable lui
ORAB 0, SP ; OR with low byte of local variable lui
STD $0801 ; put into global variable gsi