# Atmel AVR assembly language multiplying by 2

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?

• The result would be 0x06000000, so r19 through r21 will be unused. 0x03000000 is therefore not really useful to test. Use something like 0x42424242, of which you'd know the output as well, instead. Also, are you sure r18 is the MSB? I'm not familiar with AVR Assembly, but it looks like it is the LSB.
– user17592
Commented May 4, 2013 at 9:27
• @CamilStaps yeah, i'm not too great with AVR Assembly either, I was told that r18 is the MSB, perhaps it should be at the bottom, And yeah I know those registers are unused, I just wanted an easy number so I wouldn't have to calculate it
– Sim
Commented May 4, 2013 at 9:30
• We'll see what the AVR experts say. Btw, multiplying 0x42424242 by two is quite easy too, would be just 0x84848484 ;)
– user17592
Commented May 4, 2013 at 9:32
• @CamilStaps Yeah I guess, i get a little confused when reading across the 4 registers, cause it'll only give me a hex representation and i'll have to add 4 of them up is all. (I think, am a bit rubbish at assembly at the moment :s)
– Sim
Commented May 4, 2013 at 9:33

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.

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

• A picture says more than a thousand words, nice explanation.
– user17592
Commented May 4, 2013 at 10:20
• @jippie thanks a million, really helped out cheers! Are you sure about the LSB and MSB i swear MSB goes at the top but maybe I understood that wrongly from my notes
– Sim
Commented May 4, 2013 at 10:39
• What do you mean by 'MSB goes at the top'? Commented May 4, 2013 at 11:15
• @jippie nvm i just sorted everything out, i thought the mSB got lsl'd but I realize why it doesn't now, thank you!
– Sim
Commented May 4, 2013 at 11:19