Electronic – Unsigned 32-bit multiply in AVR assembly

assemblyavr

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?

Best Answer

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