107
AT94KAL Series FPSLIC
Rev. 1138G–FPSLI–11/03
8-bit Multiplication
Doing an 8-bit multiply using the hardware multiplier is simple: just load the operands into two
registers (or only one for square multiply) and execute one of the multiply instructions. The
result will be placed in register pair R1:R0. However, note that only the MUL instruction does
not have register usage restrictions. Figure 59 shows the valid (operand) register usage for
each of the multiply instructions.
Example 1 –
Basic Usage
The first example shows an assembly code that reads the port B input value and multiplies this
value with a constant (5) before storing the result in register pair R17:R16.
in
r16,PINB
; Read pin values
ldi
r17,5
; Load 5 into r17
mul
r16,r17
; r1:r0 = r17 * r16
movw
r17:r16,r1:r0; Move the result to the r17:r16
; register pair
Note the use of the MOVW instruction. This example is valid for all of the multiply instructions.
Figure 59.
Valid Register Usage
Example 2 –
Special Cases
This example shows some special cases of the MUL instruction that are valid.
lds
r0,variableA; Load r0 with SRAM variable A
lds
r1,variableB; Load r1 with SRAM variable B
mul
r1,r0
; r1:r0 = variable A * variable B
lds
r0,variableA; Load r0 with SRAM variable A
mul
r0,r0
; r0:r0 = square(variable A)
Even though the operand is put in the result register pair R1:R0, the operation gives the cor-
rect result since R1 and R0 are fetched in the first clock cycle and the result is stored back in
the second clock cycle.
R0
R1
R2
R3
R4
R6
R5
R7
R8
R9
R10
R11
R12
R14
R13
R15
R16
R17
R18
R19
R20
R22
R21
R23
R24
R25
R26
R27
R28
R30
R29
R31
MUL
R16
R17
R18
R19
R20
R22
R21
R23
R24
R25
R26
R27
R28
R30
R29
R31
R16
R17
R18
R19
R20
R22
R21
R23
MULS
MULSU
FMUL
FMULS
FMULSU