gcc - 将变量地址加载到寄存器 PowerPC 内联汇编
问题描述
我正在尝试将在“C”程序中编码内联汇编代码的示例放在一起。我没有任何成功。我正在使用 GCC 4.9.0。据我所知,我的语法是正确的。但是,构建会因以下错误而崩溃:
/tmp/ccqC2wtq.s:48: Error: syntax error; found `(', expected `,'
Makefile:51: recipe for target 'all' failed
/tmp/ccqC2wtq.s:48: Error: junk at end of line: `(31)'
/tmp/ccqC2wtq.s:49: Error: syntax error; found `(', expected `,'
/tmp/ccqC2wtq.s:49: Error: junk at end of line: `(9)'
这些与代码中的输入/输出/clobbers 行有关。有人知道我哪里出错了吗?
asm volatile("li 7, %0\n" // load destination address
"li 8, %1\n" // load source address
"li 9, 100\n" // load count
// save source address for return
"mr 3,7\n"
// prepare for loop
"subi 8,8,1\n"
"subi 9,9,1\n"
// perform copy
"1:\n"
"lbzu 10,2(8)\n"
"stbu 10,2(7)\n"
"subi 9,9,1\n" // Decrement the count
"bne 1b\n" // If zero, we've finished
"blr\n"
: // no outputs
: "m" (array), "m" (stringArray)
: "r7", "r8"
);
解决方案
目前尚不清楚您要如何处理初始说明:
li 7, %0
li 8, %1
您想将变量的地址加载到这些寄存器中吗?通常,这是不可能的,因为地址不能在立即操作数中表示。最简单的方法是避免使用 r7 和 r8,而是直接使用 %0 和 %1 作为寄存器名称。似乎您想将这些寄存器用作基地址,因此您应该使用b
约束:
: "b" (array), "b" (stringArray)
然后 GCC 将以适当的方式处理具体化地址的细节。
您不能使用blr
从内联汇编器返回,因为无法拆除 GCC 创建的堆栈帧。您还需要仔细检查clobbers 并确保列出所有您clobber 的内容(包括条件代码、内存和覆盖的输入操作数)。