Page 58 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 58
2.2 Arithmetic Instructions 35
Figure 2.5. Program Segment for 32-Bit Addition
Figure 2.5 illustrates a simple example of an arithmetic operation: adding a 4-byte
number at $850 to a 4-byte number at $854. ADDD can be used to add the two low-order
bytes, but ADCB and ADCA are needed to add the high-order bytes.
Arithmetic instructions are really very simple and intuitively obvious, except for the
condition code bits. Addition or subtraction uses the same instruction for unsigned as for
two's-complement numbers, but the test for overflow is different (see Appendix 1). The
programmer has to use the correct condition code test after instruction completion; for
example, SUBA $876 sets C = 1 if, and only if, there has been an unsigned overflow;
that is, A - ($876) produces a borrow or, when each number is treated as an unsigned
number, A < ($876). (Here A and ($876) denote the contents of A and the contents of
location $876.) Similarly, V = 1 if, and only if, a two's-complement (signed) overflow
occurs, when A and ($876) are treated as two's-complement numbers; i.e., A - ($876) is
not in the 8-bit two's-complement range. Note again that subtraction is performed with
the same instruction, such as SUBA f regardless of whether the numbers are two's-
complement or unsigned numbers.
Table 2.5 shows special instructions used to improve efficiency for commonly used
operations. ABX, ABY, ABA, CBA, and SBA use accumulator B, and DBS, DEX,
DEY, INS, INK, and INY increment or decrement index registers and the stack
pointer.
Multiply instructions MUL and EMUL multiply unsigned numbers in specific
registers. EMULS similarly multiplies signed numbers. One may also multiply a signed
or unsigned number by two with the arithmetic shift-left instructions discussed with the
edit class, such as ASLA, ASLB, ASLD, and ASL 527. Divide instructions IDIV,
FDIV, and EDIV divide unsigned numbers in specific registers. IDIVS similarly
divides signed numbers. One may divide a two's-complement number by two with
corresponding arithmetic shift-right instructions, e .g., ASRA, ASRB, and ASR 327.
0110 0110 0110 0110
1010 1010 1010 1010
0110 01100 011110 0111100
add top number to 0 shift it left shift left, add top shift left
Figure 2.6. Multiplication