Arduino inline assembly: 16 bit x 8 bit multiplication!

Hmm - I was impressed with tim7's code until I considered that a*5 could be wider than 16 bits, in which case the algorithm would produce incorrect results. Interestingly, avr-gcc was willing to do the unsigned int computation and return the (possibly incorrect) result as an unsigned long. To force a correct result, I had to cast a to unsigned long before the multiplication:

unsigned long mul5m(unsigned a)
{  return (unsigned long) a * 5;
}

which produced:

mul5m:
  12               	/* prologue: function */
  13               	/* frame size = 0 */
  14 0000 A0E0      		ldi r26,lo8(0)
  15 0002 B0E0      		ldi r27,hi8(0)
  16 0004 282F      		mov r18,r24
  17 0006 392F      		mov r19,r25
  18 0008 4A2F      		mov r20,r26
  19 000a 5B2F      		mov r21,r27
  20 000c 220F      		lsl r18
  21 000e 331F      		rol r19
  22 0010 441F      		rol r20
  23 0012 551F      		rol r21
  24 0014 220F      		lsl r18
  25 0016 331F      		rol r19
  26 0018 441F      		rol r20
  27 001a 551F      		rol r21
  28 001c 280F      		add r18,r24
  29 001e 391F      		adc r19,r25
  30 0020 4A1F      		adc r20,r26
  31 0022 5B1F      		adc r21,r27
  32 0024 622F      		mov r22,r18
  33 0026 732F      		mov r23,r19
  34 0028 842F      		mov r24,r20
  35 002a 952F      		mov r25,r21
  36               	/* epilogue start */
  37 002c 0895      		ret

which is longer, but doesn't discard any high-order result bits.