# Unsigned 32-bit multiply in AVR assembly

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?

## 2 Answers

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'.

• 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. Commented Apr 29, 2018 at 12:34
• @JaySun - The rotate to the left is the correct concept. But in your question you stated rotating the bytes in the wrong order. Commented Apr 29, 2018 at 12:51
• 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 Commented Apr 29, 2018 at 12:52
• @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. Commented Apr 29, 2018 at 12:54

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

LSL R21
ROL R20
ROL R19
ROL R18