下面是我用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]]'

GCC抱怨ldrh %[tmp2], [%2]行,但是我不明白为什么。我看了一下LDRH指令,在我看来我的指令模板是正确的。
Load memory halfword [15:0] from register address + 5-bit immediate offset
LDRH <Rd>, [<Rn>, #<immed_5> * 2]

顺便说一句,这是我用来编译此命令:
arm-none-linux-gnueabi-gcc -O2 -march=armv7-a -mthumb

最佳答案

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])
:
);

07-24 14:32