Page 288 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 288
9,3 Conditional Statements 265
The statement lui = (lui « 3) + (lui « 1) + Isc -' 0 ' ; in Figure 9.5
which can be used to build a decimal number from ASCII characters, is compiled into
STD 0, SP ; save lui which was left in D
LSLD ; shift left three places
LSLD ; in order to
LSLD ; multiply by eight
TFR D, X ; save this intermediate result in a register
PULD ; get lui again.
LSLD ; shift left to double it
LEAX D, X ; add both parts
TFR X, D ; move to D to complete the addition
ADDD 0, SP ; add Isc, which is left on the stack
SUED #48 ; subtract the constant for ASCII '0'
STD 4, SP ; save result in lui: note the offset
Temporary results can be saved in registers, as we saw in the TFR D, X instruction. The
stack provides another place to temporarily save the data in accumulator D and can save
essentially any number of such values. Note again that the stack pointer offset changes
as temporary results are saved on the stack. At the subroutine's end, the stack pointer is
adjusted, not only to deallocate local variables but also to deallocate temporary variables,
using the instruction LEAS 7,SP. Deallocating at the end saves instructions that
should deallocate temporary variables when they are no longer needed, to improve static
efficiency, at the expense of using up more of the stack than would be needed if
temporary variables were promptly deallocated when they were no longer needed.
9.3 Conditional Statements
A statement can be conditional, or it can control looping to execute a sequence of
statements that are written within it many times. We first present Boolean operators that
generate a 1 (true) or 0 (false) variable. We then give assembly-language program
segments for an example of several of C's control statements.
To illustrate Boolean operators, the expression main in Figure 9.6 compares some
variables. Many branch instructions such as BEQ *+5 are used to indicate a branch that
is five bytes ahead of the (beginning of the) BEQ instruction. This current location
counter is used to avoid generating a lot of labels for local branching.
In Figure 9.6, the C procedure's first expression guc = Isc > -3; results in
LDAA 3, -SP ; allocate 3 bytes for local variables and get variable Isc
CMPA #253 ; if greater than -3 as a signed number
BGT *+4 ; then proceed to "true" program segment
CLRA ; if false, clear guc. If true,
CPS #34305 ; then skip over operand jump to operand which is LDAA #1
S TAA $ 0 8 0 0 ; store the result