Page 291 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 291
268 Chapter 9 Implementation of C Procedures
Certain bit tests can often use BRSET or BRCLR branches. In Figure 9.6, the
expression Isc = (gsi & 4) == 0; results in the following code:
BRCLR $0802, #4 , *+2 ;if bit 4 is not zero then
CLRA ; clear result
GPS #34305 ; skip, or set result to 1
STAA 3, SP+ ; put result in lui and deallocate local variables
The Boolean result of the test, a value of 1 (T) or a 0 (F), is actually not usually
generated but may be used to branch to a different location. For instance if {lui > 5)
results in the following code:
LDD 1, SP ; get 16-bit variable lui
CPD #5 ; if less than 5 as an unsigned number
BLS L ; branch around the expression if lui is higher than 5
Simple conditional expressions of the form if then, full conditionals of the form if
then else, and extended conditionals of the form if then else if then else if then , . .
else, use conditional expression operators. In the last expression, the else if part can be
repeated as many times as needed, and the last part can be an optional else. Variables are
compared using relational operators ( > and < ), and these are combined using logical
operators (&&). We give examples of common simple conditionals first.
The C program in Figure 9.7a is compiled into the assembly language program
shown in Figure 9.7b. A statement if (! Isc) guc = 0; or equivalently if (Isc ==
0) guc = 0; is encoded as
LDAA 3, -SP ; allocate and set condition codes for variable Isc
BNE *+5 ; if nonzero, skip over next instruction
CLR $0800 ; otherwise, if zero, clear variable guc
Where the condition applies to a complex expression of many statements, the
branch instructions can be converted to long branch instructions. For instance,
if (gsi < 5) { ... /* many instructions */ }
can be implemented
LDD $0801 ;get variable gsi
CPD #5 ; if greater than or equal to 5 as an unsigned number
LBGE LI ; then skip over next several instructions
; many instructions generated between { } appear here
LI: EQU * ; located after the latter} matching the if statement's {
A simple C compiler can always implement the conditional operation using the
long branch instructions like LBHS, but an optimizing C compiler will get the size of
the branch offset. It uses a long branch instruction when the label cannot be reached by
the corresponding shorter branch instruction.