Path: Home => AVR-Overview => Binary calculations => Multiplication

Binary multiplication in AVR Assembler

Decimal multiplication

In order to multiply two 8-bit-binaries we remind ourselves, how this is done with decimal numbers:

    1234 * 567 = ?
    1234 *   7 =    8638
+  12340 *  6  =   74040
+ 123400 * 5   =  617000
    1234 * 567 =  699678

In single steps decimal:
  1. We multiply the first number by the lowest significant digit of the second number and add this to the result.
  2. We multiply the first number by 10 and then by the next higher digit of the second number and add to the result.
  3. We multiply the first number by 100, then with the third-highest digit, and add this to the result.

Binary multiplication

Now in binary. Multiplication with the single digits is not necessary, because there are only the digits 1 (add the number) and 0 (don't add the number). Multiplication by 10 in decimal goes to multiplication by 2 in binary mode. Multiplication by 2 is done easily, either by adding the number with itself, or by shifting all bits one position left and writing a 0 to the void position on the right. You see that binary math is very much easier than decimal. Why didn't mankind use this from the beginning?

AVR-Assembler program

The program for multiplication in AVR assembler language is demonstrated in HTML following this link, the source code is available at this link. This source code can be assembled, the resulting object-file can be opened in the simulator studio provided as free software by ATMEL. These are the positions and registers used in the program:

.rmh = R1 = 0x00rm1 = R0 = 0xAA
Z100000000 10101010
*.rm2 = R2 = 0x55
Z201010 101
=reh = R4 = 0x00rel = R3 = 0x00
Erg00000000 00000000

Binary rotation

For understanding the multiplication operation, it is necessary to understand the binary rotation commands ROL and ROR. These instructions shift all bits of a register one position left (ROL) resp. right (ROR). The void position in the register is filled with the content of the carry bit of the status register, the bit that rolls out of the register is shifted to the carry bit. This operation is demonstrated using 0xAA as an example for ROL and 0x55 as an example for ROR:


Multiplication in the studio

The following screenshots show the multiplication program in the simulator.
  1. MULT8E1.gif: The object-code has been opened, the cursor is placed on the first executable instruction. F11 does single steps.
  2. MULT8E2.gif: The registers R0 and R2 are set to 0xAA and 0x55, our test binaries, to be multiplied.
  3. MULT8E3.gif: R2 is rotated to the right, to roll the least significant bit into the carry bit. 0x55 (0101.0101) yielded 0x2A (0010.1010).
  4. MULT8E4.gif: Because the carry bit had a one the content of the registers R1:R0 is added to the (empty) register pair R4:R3, resulting in 0x00AA there.
  5. MULT8E5.gif: Now the register pair R1:R0 is rotated one position left to multiply this binary by 2. From 0x00AA, multiplication by 2 yields 0x0154.
  6. The whole multiplication loop is repeated as long there is at least one binary 1 in register R2. These following loops are not shown here.
  7. MULT8E6.gif: Using key F5 of the studio we multi-stepped over these loops to a breakpoint at the end of the multiplication routine.
  8. MULTBE7.gif: The result register pair R4:R3 has the result of the multiplication of 0xAA by 0x55: 0x3872.
This wasn't that complicated, just remind yourself on the similiar decimal operations. Binary multiplication is much easier than decimal.

To the top of that page

©2002 by