Electronic – Atmel AVR assembly language multiplying by 2

assemblyatmelavr

I'm trying to multiply a 32-bit unsigned number by 2 across registers r18, 19, 20 and 21, r18 being the most significant byte. This is my code:

ldi r18, 0x03
ldi r19, 0x00
ldi r20, 0x00
ldi r21, 0x00
lsl r18
ror r19
ror r20
ror r21

Just used 3 for an easy example so I will know the output. I can't work out whether it's ror or rol for the rest of the registers. For a 16-bit number you would use ror to account for a carry, does this also apply for unsigned 32-bit numbers?

Am I supposed to use ror or rol?

Best Answer

Refer to the AVR Instruction Set document.

For the least significant byte you want a LSL – Logical Shift Left. Shift a 0 in as least significant bit and remember the highest bit in the Carry flag.

Then for the subsequent higher bytes, you want a ROL – Rotate Left trough Carry. You shift the remembered bit from carry into the right (least significant) bit and the bit that is shifted out at the high side is pushed into carry.

You chose to use R18 as LSB and R21 as MSB, nothing wrong with that.

times2

Read the above image from top right (LSB is being processed first) to bottom left.