Page 206 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 206
7.2 Integer Conversion 183
*
* SGNMUL multiplies the 1-byte signed number in B by the 1-byte signed number in
* A, putting the product in accumulator D. Registers X and Y are unchanged.
*
SGNMUL: PSHD ; Save two bytes to be multiplied
MUL ; Execute unsigned multiplication
TST 1, SP ; If first number is negative
BPL LI ; Then
SUBA 0, SP ; Subtract second number
LI: TST 0,SP ;If second number is negative
BPL L2 ; Then
SUBA 1, SP ; Subtract first number
L2: LEAS 2, SP ; Balance stack
RTS ; Return with product in accumulator D
a. Using MUL
*
* SGNMUL multiplies the 1-byte signed number in B by the 1-byte signed number in
* A, putting the product in accumulator D. Register X is unchanged.
*
SGNMUL: SEX A, Y ; move one multiplier to Y, sign extending it
SEX B f D ; sign extend the other multiplier
EMULS ; put the low-order 16-bits in D
RTS ; Return with product in accumulator D
b. Using EMULS
Figure 73.8-Bit Signed Multiply Subroutine
Another approach to multiplication of signed 8-bit numbers is to use signed 16-bit
multiplication available in the EMULS instruction. See Figure 7.3b. This method is far
better on the 6812, because the EMULS instruction is available, but the former method is
useful on other machines and shows how signed multiplication is derived from unsigned
multiplication having the same precision. A modification of it is used to multiply 32-bit
signed numbers, using a procedure to multiply 32-bit unsigned numbers.
7.2 Integer Conversion
A microcomputer frequently communicates with the outside world through ASCII
characters. For example, subroutines INCH and DUTCH allow the MPU to communicate
with a terminal, and this communication is done with ASCII characters. When numbers
are being transferred between the MPU and a terminal, they are almost always decimal
numbers. For example, one may input the number 3275 from the terminal keyboard,
using the subroutine INCH, and store these four ASCII decimal digits in a buffer. After
the digits are input, the contents of the buffer would be