0

In my computer science class, we just started working in AVR this week. I am having trouble with the bit manipulation required to convert an 8-bit int to a 16-bit int. For context, I am running the program off an arduino.

This is what I have so far:

.global int8ToInt
int8ToInt:
sbrc r24,7
ldi r25,127
cp r25,r24
sbi r25,r25
ret

This is just what I have from looking a bit at docs and similar problems on this website, but I do not know where to go from here, or even if this is quite right. Any help would be greatly appreciated!

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 2
    The simplest is to `clr r25; sbrc r24, 7; dec r25`. – Jester Nov 30 '20 at 19:06
  • Just those three? Or adding those 3 as lines after the ```sbi r25```? – Anorak Kingvid Nov 30 '20 at 19:07
  • 1
    Just those. Actually, `mov r25, r24; asr r25, 7` is even simpler. – Jester Nov 30 '20 at 19:09
  • Thank you so much! – Anorak Kingvid Nov 30 '20 at 19:10
  • 2
    You can always ask a compiler. gcc seems to do `mov r0, r24; lsl r0; sbc r25,r25`. – Jester Nov 30 '20 at 19:12
  • 1
    @Jester: Is there no shorter way to get CF set according to the high bit of r24? Like cmp-immediate? No probably not, you'd need a reverse-subtract `0x7f - x` - ARM has that, AVR doesn't. Or an `add reg, 0x80` but add writes the register. (re: `asr r25, 7` - remember that AVR only has shift-by-1. If that assembles at all, it would be a pseudo-instruction for 7 ASRs, which is horrible :/) – Peter Cordes Dec 01 '20 at 01:15
  • Note that zero-extension (unsigned) is much easier, just clear the high half. If you know your `int8` values are always non-negative, you can do that. – Peter Cordes Dec 01 '20 at 01:18
  • Ah yeah, no multi-bit `ASR`. – Jester Dec 01 '20 at 01:30
  • There is `BST` but unfortunately that writes the special T flag not C. – Jester Dec 01 '20 at 01:34
  • @Jester there's no barrel shifter in tiny microcontrollers like AVR, so the [AVR can only shift 1 bit at a time](https://stackoverflow.com/a/17908514/995714), there's no `ASR r25, 7` – phuclv Dec 01 '20 at 02:09
  • check the output from GCC https://gcc.godbolt.org/z/Pe1rnf – phuclv Dec 01 '20 at 02:14

0 Answers0