108
AT94KAL Series FPSLIC
Rev. 1138G–FPSLI–11/03
Example 3 – Multiply-
accumulate Operation
The final example of 8-bit multiplication shows a multiply-accumulate operation. The general
formula can be written as:
; r17:r16 = r18 * r19 + r17:r16
in
r18,PINB
; Get the current pin value on port B
ldi
r19,b
; Load constant b into r19
muls
r19,r18
; r1:r0 = variable A * variable B
add
r16,r0
; r17:r16 += r1:r0
adc
r17,r1
Typical applications for the multiply-accumulate operation are FIR (Finite Impulse Response)
and IIR (Infinite Impulse Response) filters, PID regulators and FFT (Fast Fourier Transform).
For these applications the FMULS instruction is particularly useful. The main advantage of
using the FMULS instruction instead of the MULS instruction is that the 16-bit result of the
FMULS operation always may be approximated to a (well-defined) 8-bit format, see “Using
Fractional Numbers” on page 111.
16-bit Multiplication
The new multiply instructions are specifically designed to improve 16-bit multiplication. This
section presents solutions for using the hardware multiplier to do multiplication with 16-bit
operands.
Figure 60 schematically illustrates the general algorithm for multiplying two 16-bit numbers
with a 32-bit result (C = A • B). AH denotes the high byte and AL the low byte of the A operand.
CMH denotes the middle high byte and CML the middle low byte of the result C. Equal nota-
tions are used for the remaining bytes.
The algorithm is basic for all multiplication. All of the partial 16-bit results are shifted and
added together. The sign extension is necessary for signed numbers only, but note that the
carry propagation must still be done for unsigned numbers.
Figure 60.
16-bit Multiplication, General Algorithm
c n
( )
a n
( )
b
c n
1
–
(
)
+
×
=
AH
AL
BH
BL
X
AL * BL
AL * BH
AH * BL
AH * BH
+
+
+
=
CH
CMH
CML
CL
(sign ext)
(sign
ext)
(sign
ext)