0

I stumbled upon some strange compiling errors when using SBIW (Subtract Immediate from Word) in inline assembly. I want to use a register pair to do some simple 16-bit arithmetic (just incrementing/decrementing). I followed the instruction reference Instruction-set, p. 119 and used the code example shown on the page for my inline assembly:

asm volatile(
  "ldi r25, 0xff  \n"
  "ldi r24, 0xff  \n"
  "sbiw r25:r24,1 \n"
);

This snippet does not compile and throws the error: register r24, r26, r28 or r30 required Switching r25 and r24, I get:

asm volatile(
  "ldi r25, 0xff   \n"
  "ldi r24, 0xff   \n"
  "sbiw r24:r25, 1 \n"
);

still not compiling with an error `,' required, leading to:

asm volatile(
  "ldi r25, 0xff   \n"
  "ldi r24, 0xff   \n"
  "sbiw r24, 1     \n"
);

which is compiling without an error. What am I overseeing here? Might add that I am not that versatile with asm, so maybe I just got the documentation wrong.

On a sidenote: are sbiw/adiw good choices for simple 16 bit arithmetic? Thanks for your input!

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
rikisa
  • 301
  • 2
  • 9
  • 2
    Nothing, your assembler simply wants that syntax. `sbiw` automatically operates on consecutive registers, so it's not really necessary to give both. – Jester Apr 11 '16 at 23:37
  • 1
    It's usually better to let the compiler do as much work as possible. Tell it you want 0xff in two C variables that you use as read-write operands. I hope you're just simplifying to remove your input/output operands for the SO post... – Peter Cordes Apr 12 '16 at 01:12
  • @PeterCordes Indeed, I removed input/output/clobbers for simplicity sake. But I'll keep your hint in mind. Cheers, also to Jester – rikisa Apr 12 '16 at 01:23
  • 1
    I've written some answers about using GNU C inline asm to make code that doesn't suck. Most of it is applicable to non-x86, even though all the examples are x86. I put a link collection [at the end of an answer where it seemed relevant](http://stackoverflow.com/questions/34520013/using-base-pointer-register-in-c-inline-asm/34522750#34522750). – Peter Cordes Apr 12 '16 at 01:33

0 Answers0