MUL - multiplication instruction in microcontrollers, is present, for example, microcontrollers Atmega. But not all AVR microcontrollers there is hardware support multiplication of functions. For example, in microcontrollers Attiny MUL team simply does not exist, so the solution to this problem lies with the program code.
The main point to be taken into account in the implementation of the multiplication function is that to save the multiplication result will need two registers. In one of the registers will be kept low byte of the result, and in the second case bude saved significant byte result.
Software implementation MUL
Below is a function of multiplication, which works with 8-bit numbers and the output provides a 16-bit number.
;======= Software multiplication function (implementing mul) ============================ ; r16 ; multiplicand ; r17 ; multiplier ; r0 ; the low byte of the result ; r1 ; the high byte of result ; r19 ; cycle counter ;----------------------------------------------------------------------------------- mpy8u_c: clr r1 ; clear the register to store the result of the high byte ldi r19,8 ; We set the starting value of the loop counter lsr r17 ; right shift multiplier m8u_1: brcc m8u_2 ; We make a transition if С = 0 add r1,r16 ; to the high byte of the result we add the multiplicand m8u_2: ror r1 ; high byte of the result is shifted to the right ror r0 ; low byte result is shifted to the right dec r19 ; We produce a decrement counter brne m8u_1 ; We make a transition if С != 0 ret
Note. This software function used arithmetic operations, control of execution of which is assigned to the SREG - AVR Status register.
SREG - AVR Status Register
-
Bit 7
- Bit globally enable interrupts;
Bit 6 - Bit copying and downloading instructions;
Bit 5 - Carry flag. Exhibited after some arithmetic operations;
Bit 4 - Flag of the sign;
Bit 3 - additions flag to two. Exhibited overflow registers;
Bit 2 - Flag negative result of arithmetic or logic operations;
Bit 1 - zero result flag. Exhibited, if the result of arithmetic or logic operation is zero.
Bit 0 - Carry flag. Indicates transfer in the arithmetic and logic operations.