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

Binary division in AVR Assembler

Decimal division

Again we start with the decimal division, to better understand the binary division. We assume a division of 5678 by 12. This is done like this:

             5678 : 12 = ?
- 4 * 1200 = 4800
- 7 *  120 =  840
- 3 *   12 =   36
Result: 5678 : 12 = 473 Remainder 2

Binary division

In binary the multiplication of the second number (4 * 1200, etc.) is not necessary, due to the fact that we have only 0 and 1 as digits. Unfortunately binary numbers have much more single digits than their decimal equivalent, so transferring the decimal division to its binary equivalent is a little bit inconvenient. So the program works a bit different than that.

The division of a 16-bit binary number by a 8-bit binary in AVR assembler is listed in HTML format following this link. The source code is at this link.

Program steps during division

During execution of the program the following steps are ran:
  1. Definition and preset of the registers with the test binaries,
  2. presetting the interim register and the result register pair (the result registers are presetted to 0x0001! After 16 rotations the rolling out of the one stops further division steps.),
  3. the 16-bit-binary in rd1h:rd1l is rotated bitwise to the interim register rd1u (multiplication by 2), if a 1 is rotated out of rd1u, the program branches to the subtraction step in step 4 immediately,
  4. the content of the interim register is compared with the 8-bit binarly in rd2, if rd2 is smaller it is subtracted from the interim register and the carry-bit is set to one, if rd2 is greater the subtraction is skipped and a zero is set to the carry flag,
  5. the content of the carry flag is rotated into the result register reh:rel from the right,
  6. if a zero rotated out of the result register, we have to repeat the division loop, if it was a one the division is completed.
If you don't understand rotation yet you'll find this operation discussed in the multiplication section.

Division in the simulator

The following screen shots demonstrate the program steps in the studio. To do this, you have to assemble the source code and open the resulting object file in the studio.
  1. DIV8E1.gif: The object code has been started, the cursor is on the first executable instruction. The key F11 performs single steps.
  2. DIV8E2.gif: The test binaries 0xAAAA and 0x55, to be divided, are written to the registers R1:R0 and R3.
  3. DIV8E3.gif: The interim register R2 and the result register pair are set to their predfined values.
  4. DIV8E4.gif: R1:R0 was rotated left to R2, from 0xAAAA the doubled value of 0x015554 was yielded.
  5. DIV8E5.gif: No overflow from rotation into carry has occurred and 0x01 in R2 was smaller than 0x55 in R3, so subtraction was skipped. A zero in the carry is rotated into the result register R5:R4. The former content of the result register, a single 1-bit in position 0 has rotated to position 1 (content now: 0x0002). As a zero was rotated out of the result register pair, the next step to be executed is a branch to the beginning of the division loop start and the loop is repeated.
  6. DIV8E6.gif: After executing the loop 16 times we have reached the breakpoint set at the end of the division routine. The result register in R5:R4 holds 0x0202, the result of the division. The registers R2:R1:R0 are empty, so we do not have a remainder left. If a remainder would have been resulted we can use it to decide whether an incrementation of the result should take place, rounding of the result up. This step is not coded here.
  7. DIV8E7.gif: The whole division needs 60 micro-seconds processor time (open a processor view in the studio menue). A rather long time for a division.

To the top of that page

©2002 by