gcc内联汇编程序获取错误“asm语句中需要左值”

时间:2012-11-21 19:42:36

标签: gcc inline-assembly avr

我需要编写

的C代码
DDRB &= ~pins;

内联汇编(AVR)。我之前从未使用过它。我的尝试:

register uint8_t t1, t2; // temporary registers
__asm__ volatile (
    "in %[t1], %[w1_ddr]"   "\n\t"
    "mov %[t2], %[pins]"    "\n\t"
    "com %[t2]"             "\n\t"
    "and %[t1], %[t2]"      "\n\t"
    "out %[w1_ddr], %[t1]"  "\n\t"
    : [t1] "+r" (t1),
      [t2] "+r" (t2),
      [w1_ddr] "+I" (_SFR_IO_ADDR(DDRB))
    : [pins] "r" (pins)
);  

gcc出现以下错误lvalue required in asm statement。我做错了什么?

1 个答案:

答案 0 :(得分:1)

AVR中的“I”约束表示操作数是常量。因此它不能是“+”,即输入 - 输出。明白输入,如下:

register uint8_t t1, t2; // temporary registers
__asm__ volatile (
    "in %[t1], %[w1_ddr]"   "\n\t"
    "mov %[t2], %[pins]"    "\n\t"
    "com %[t2]"             "\n\t"
    "and %[t1], %[t2]"      "\n\t"
    "out %[w1_ddr], %[t1]"  "\n\t"
    : [t1] "+r" (t1),
      [t2] "+r" (t2)
    : [w1_ddr] "I" (_SFR_IO_ADDR(DDRB)),
      [pins] "r" (pins)
);