2
\$\begingroup\$

How do I perform a multiplication by 2 of the unsigned 32-bit quantity in registers r18:r19:r20:r21 where r18 is the most significant byte?

My answer is this: rol r18; rol r19; rol r20; rol r21.

My thought process is that since this is an unsigned number, we can exclude the use of ASL as there is no carry out in MSB. But I didn't get it right; may I know which part is wrong?

\$\endgroup\$
5
\$\begingroup\$

To do this correctly for multiplying a 4-byte value by two you have to ensure that the low bit of the low byte gets a '0' shifted into it. In the AVR instruction set this can be achieved with the following sequence of instructions assuming a value in r18:r19:r20:r21.

CLC
ROL R21
ROL R20
ROL R19
ROL R18

Note the need to shift first the low byte and then to propagate bits through carry for the rest of the shifts. The first CLC instruction takes care of assuring that the LSB of R21 ends up being '0'.

\$\endgroup\$
  • \$\begingroup\$ So what i did was actually correct? because base on my assumption, If it is unsigned then i would ignore the carry out. Therefore a simple rotate to the left will solve this problem. \$\endgroup\$ – Jay Sun Apr 29 '18 at 12:34
  • 1
    \$\begingroup\$ @JaySun - The rotate to the left is the correct concept. But in your question you stated rotating the bytes in the wrong order. \$\endgroup\$ – Michael Karas Apr 29 '18 at 12:51
  • \$\begingroup\$ Oh i get it, in the term of multiplication, We have to go from the LSB instead of MSB. please correct me if i am wrong \$\endgroup\$ – Jay Sun Apr 29 '18 at 12:52
  • \$\begingroup\$ @JaySun - Look at my answer. I showed the correct order there. In general when you right shift you start with the higher order byte and when left shifting you start with the lower order byte. \$\endgroup\$ – Michael Karas Apr 29 '18 at 12:54
0
\$\begingroup\$

Save one instruction. LSL shifts left inserting a zero bit into LSBit of R21.

LSL R21
ROL R20
ROL R19
ROL R18
\$\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.