# AVR assembly language on Division

how do i performs a division by 4 of the 16-bit unsigned value in registers r4:r5 Where r4 is the most significant bit.

I understand that by division of 4 which means i have to shift the bit by 2. but how do i do it in assembly language? can i basically just LSL r4;LSLr5;? thank you for your help in explaining this.

• Did you not learn from your previous two questions? – Ignacio Vazquez-Abrams May 7 '18 at 10:23
• @IgnacioVazquez-Abrams still bit confusing in doing this, i understand how it works, but i am not confident how do i exactly put it in assembly language. – Jay Sun May 7 '18 at 10:31
• Why can't you simply test it and see if you got it right? – pipe May 7 '18 at 13:25
• Clear the carry bit, shift each byte right starting from the MSB, then do it twice. – alex.forencich May 7 '18 at 13:26

To divide by two:

LSR r4 which push b0 to to carry b1 to b0, ... b7 to b6 and zero to b7

ROR r5 which will push b0 to to carry b1 to b0, ... b7 to b6 and carry that is the b0 from r4 to b7

To divide by 4 use the sequence twice

LSL is used for multiplication.

The pictures are from Microchip Website AVR Assembler Instructions

For example if the value is in the R24/R25 pair.

    lsr r25
ror r24
lsr r25
ror r24


Just divide by two, twice. Each divide by two is a right shift by one bit. Don't forget to clear the carry flag before shifting. Shifting twice can be done either in a loop or unrolled. In this case, unrolled may be more efficient.