1
\$\begingroup\$

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.

\$\endgroup\$
  • 3
    \$\begingroup\$ Did you not learn from your previous two questions? \$\endgroup\$ – Ignacio Vazquez-Abrams May 7 '18 at 10:23
  • \$\begingroup\$ @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. \$\endgroup\$ – Jay Sun May 7 '18 at 10:31
  • 1
    \$\begingroup\$ Why can't you simply test it and see if you got it right? \$\endgroup\$ – pipe May 7 '18 at 13:25
  • \$\begingroup\$ Clear the carry bit, shift each byte right starting from the MSB, then do it twice. \$\endgroup\$ – alex.forencich May 7 '18 at 13:26
2
\$\begingroup\$

To divide by two:

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

enter image description here

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

enter image description here

To divide by 4 use the sequence twice

LSL is used for multiplication.

The pictures are from Microchip Website AVR Assembler Instructions

\$\endgroup\$
2
\$\begingroup\$

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

    lsr r25
    ror r24
    lsr r25
    ror r24
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.