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?

  • \$\begingroup\$ 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. \$\endgroup\$ – Keelan May 4 '13 at 9:27
  • \$\begingroup\$ @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 \$\endgroup\$ – Sim May 4 '13 at 9:30
  • \$\begingroup\$ We'll see what the AVR experts say. Btw, multiplying 0x42424242 by two is quite easy too, would be just 0x84848484 ;) \$\endgroup\$ – Keelan May 4 '13 at 9:32
  • \$\begingroup\$ @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) \$\endgroup\$ – Sim May 4 '13 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.

  • 1
    \$\begingroup\$ A picture says more than a thousand words, nice explanation. \$\endgroup\$ – Keelan May 4 '13 at 10:20
  • \$\begingroup\$ @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 \$\endgroup\$ – Sim May 4 '13 at 10:39
  • \$\begingroup\$ What do you mean by 'MSB goes at the top'? \$\endgroup\$ – jippie May 4 '13 at 11:15
  • \$\begingroup\$ @jippie nvm i just sorted everything out, i thought the mSB got lsl'd but I realize why it doesn't now, thank you! \$\endgroup\$ – Sim May 4 '13 at 11:19

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.