Page 280 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 280
9.2 Expressions and Assignment Statements 257
The input and output statements use page-zero addressing, which provides improved
static and dynamic efficiency over direct adddressing. Note that the MOVB instruction is
not useful for accessing these I/O ports, because there is no page-zero address option in
MOVB. The LDAB and STAB instructions above are more efficient than a MOVB
instruction.
The assignment of I/O ports to global variable names should be written and executed
before true global variables are assigned, because the origin will be set to the beginning
of RAM (at $800) to assign true global variables. The declaration of globally defined I/O
ports is often put in an #include file, which is inserted in a program before globals are
defined in the program.
9.2 Expressions and Assignment Statements
In this section, we illustrate how operators are used in expressions. We will look at
addition and subtraction statements that use same-width and different-width operands in a
discussion of upcasting and downcasting. We will then study statements that use logical
and arithmetic operators. We will carefully consider the increment and decrement
operators and then look at expressions that save temporary results on the hardware stack.
The program in Figure 9.2a has several local and global variables, some of which
are signed and others of which are unsigned, and some of which are 8-bit and others of
which are 16-bit. Figure 9.2b shows assembly language developed from this program.
Observe that each variable's name is an abbreviation of its characteristics; gsi is a
global signed integer.
Many C statements are easily and efficiently translated into assembly language. This
is especially true when all the variables in a statement are 8-bit char or unsigned
char variables or when all the variables in a statement are 16-bit int or unsigned
int variables. Assume the following statements are written in Figure 9.2a's main.
Figure 9,2's statement gsi = lui + 12; is easily encoded as
LDX 0, SP ; get 16-bit local variable lui
LEAX 12, X ; add 12 (note that this is shorter than addd #12)
STX $0801 ; put into 16-bit global variable gsi
and similarly the statement guc = Isc - 33; is simply encoded as
LDAB 2, SP ; get 8-bit local variable Isc
SUBB #33 ; subtract 33
STAB $ 0 8 0 0 ; put into 8-bit global variable guc
If a statement gets an int variable and writes a char variable, the source is truncated
when it is read. Figure 9.2's statement guc = lui + 9; is encoded as
LDAB 1, SP ; get low byte of 16-bit local variable lui
ADDB #9 ; add 9
STAB $ 0 8 0 0 ; put into 8-bit global variable guc
An optimizing compiler can change the instruction ADDD #9 to ADDB #9
because the result will not be altered (reducing the precision is called downcasting).