Page 286 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 286
9.2 Expressions and Assignment Statements 263
INC $0800 ; increment variable guc
LDAA $0800 ; get value
STAA 0, SP ; store variable Isc
while Isc = guc++; in Figure 9.4 is implemented
LDAA $ 0 8 0 0 ; get value
INC $0800 ; increment variable guc
STAA 0, SP ; store variable Isc
To discuss how the stack is used to store temporary results, we use main in Figure
9.5 as an example; it merely ORs, ANDs, and shifts some variables. This C program
main is compiled into the assembly-language program in Figure 9.5b.
A statement involving several operations saves intermediate values on the stack. If
an operand of an instruction like ADD or SUB has to be zero-filled or sign extended, then
the instruction's other operand, in accumulator B or D, may have to be temporarily
moved somewhere. It can be conveniently pushed on the stack and then pulled and
operated on. The statement lui = lui - Isc; in Figure 9.5, or equivalently, lui -=
Isc; is implemented by sign extending Isc and pushing the result on the stack. Then
the variable lui is recalled into accumulator D, and the extended value of Isc is
subtracted.
LDAA 2, SP ; get local variable Isc
SEX A,Y ; sign extend
LDD 0, SP ; get local variable lui
PSHY ; save on stack
SUBD 2, SP+ ; pull from stack, subtract from accumulator D
STD 0, SP ; put into global variable lui
From Figure 9.5, we next offer an example that inserts three bits into a 16-bit local
int. Note that, due to pushing the two-byte temporary variable on the stack, the stack
address to recall/w/is 4,SP. The statement lui = (lui & Oxfc7f) | {(Isc «
7) & 0x380); is compiled
PSHD ; save on stack (previous statement just computed lui)
LDAB #128 ; we will multiply by 2**7 to shift left
CLRA seven bits, so get this constant ready
PSHY save Isc from previous statement
EMUL shift it
ANDA #3 mask off low two bits of accumulator A
ANDB #128 mask off high bit of accumulator B
PSHD save temporary result
LDD 4, SP get lui (notice offset adjustment)
ANDB #127 mask all low-order bits
ANDA #252 mask all high-order bits
ORAA 1, SP+ combine new and old values
GRAB 1, SP+ in both bytes
STD 2, SP write out new value of lui