ARM内联汇编的GCC错误消息:预期ARM寄存器

时间:2012-07-27 22:02:00

标签: c gcc arm inline-assembly

下面是我用C和内联汇编编写的一段代码,用于将一些short值从数组blk加载到ARM R寄存器。

...
short *blk; 
... //blk memory allocation and initialization
short tmp0, tmp1, tmp2;
asm volatile (
"ldrh %[tmp0], [%0]\n\t"
"ldrh %[tmp1], [%1]\n\t"
"ldrh %[tmp2], [%2]\n\t"
: [tmp0] "=r" (tmp0), [tmp1] "=r" (tmp1), [tmp2] "=r" (tmp2)
: "m" (blk[0])  , "m" (blk[8]), "m" (blk[8*2])
: 
);

我从arm gcc 4.6

收到此错误消息
/tmp/ccDEBLCN.s:266: Error: ARM register expected -- `ldrh r3,[[r5,#0]]'

海湾合作委员会抱怨ldrh %[tmp2], [%2]行,但我不明白为什么。我看了LDRH指令,在我看来我的指令模板是正确的。

Load memory halfword [15:0] from register address + 5-bit immediate offset
LDRH <Rd>, [<Rn>, #<immed_5> * 2]

BTW这是我用来编译它的命令:

arm-none-linux-gnueabi-gcc -O2 -march=armv7-a -mthumb 

2 个答案:

答案 0 :(得分:2)

asm (
"ldrh %[tmp0], %[ref0]\n\t"
"ldrh %[tmp1], %[ref1]\n\t"
"ldrh %[tmp2], %[ref2]\n\t"
: [tmp0] "=&r" (tmp0), [tmp1] "=&r" (tmp1), [tmp2] "=r" (tmp2)
: [ref0] "m" (blk[0])  , [ref1] "m" (blk[8]), [ref2] "m" (blk[8*2])
: 
);

答案 1 :(得分:1)

看起来编译器可能会抱怨您的代码实际上没有做任何事情。这是你真正的意思吗?

...
short *blk; 
... //blk memory allocation and initialization
short tmp0, tmp1, tmp2;
asm volatile (
"ldrh r0,[%[BLK]]\n\t"
"ldrh r1,[%[BLK], #8]\n\t"
"ldrh r2,[%[BLK], #16]\n\t"
"strh r0,[%tmp0] \n\t"
"strh r1,[%tmp1] \n\t"
"strh r2,[%tmp2] \n\t"
: [tmp0] "r" (tmp0), [tmp1] "r" (tmp1), [tmp2] "r" (tmp2), [BLK] "r" (blk)
: "r0", "r1", "r2"
: 
);