Page 294 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 294
9.3 Conditional Statements 271
An operation result may be in accumulator B or D. It can be tested by the TBEQ or
TBNE instructions. In Figure 9.7, the statement if(lsc + guc) gsi = 0; similarly
encodes as
LDAA 0, SP ; get value of Isc
SEX A,D ; upcast to 16 bits
TFR D f X ; use X as accumulator
LDAB $ 0 8 0 0 ; get value of guc
CLRA ; upcast to 16 bits
LE AX D, X ; add values
TBEQ X, *+7 ; check value of sum. If zero
CLRB ; generate a 16-bit zero (A is already clear)
STD $0801 ; store to clear variable gsi
In Figure 9.7, the else part of a conditional expression is easily implemented by a
BRA instruction. The statement if (guc < 5) Isc = 0; else Isc = 9; encodes as
LDAA $ 0 8 0 0 ; get variable guc
CMPA #5 ; if greater than or equal to 5 as an unsigned number
BBS *+6 ; then skip over next instruction (this is BCC)
CLR 0, SP ; otherwise, if zero, clear variable Isc
BRA *+6 ; now skip over next two instructions
LDAB #9 ; write 9 into variable Isc
STAB 0,SP
A conditional expression can be a logical OR or a logical AND of tests described
above. The logical OR test will check each case, from left to right, for a true result, and
will execute the statement when it finds the first true result. The logical AND checks
each case, from left to right, for a false, and bypasses the statement the first time it finds
a false test. If alpha, beta, and gamma are signed global char variables, the
statement in Figure 9.8 if ((alpha < 5) && (beta ==0) ) gamma = 0; encodes as
LDAA $ 0 8 0 0 ; get variable alpha
CMPA #5 ; if less than 5 as a signed number
BGE *+10 ; then skip to CLR instruction
LDAA $ 0 8 01 ; if beta is nonzero
BNE *+5 ; then skip over next instruction
CLR $0802 ;if you get here, clear variable gamma
and if ((alpha < 5) | | (beta ==0) ) gamma = 0; is encoded as
LDAA $ 0 8 0 0 ; get variable alpha
CMPA #5 ; if less than 5 as a signed number
BLT *+7 ; then skip to CLR instruction
LDAA $ 0 8 01 ; if beta is nonzero
BNE *+5 ; then skip over next instruction
CLR $ 0 8 0 2 ; if you get here, clear variable gamma.
As seen in the previous examples, the ANDing of conditions is affected by branching
around the "then" code if either condition is false, and the ORing of conditions is affected
by branching to the "then" code if either condition is true.