问题
Using GCC and inline assembly, I want to load an immediate into a specific register r0. However, I'm not getting the right results.
unsigned short value = 0x1337;
asm volatile
(
"li 0, %0\n\t"
"sc\n\t"
"blr"
: /* Output registers */
:"r"(value) /* Input registers */
: /* No clobbered registers */
);
When compiled, this gives
li r0, 9
sc
blr
Where does the 9 come from? I wanted the specified value 0x1337 instead. Here is a tutorial I looked at.
回答1:
9 is the register containing 0x1337, which is exactly what you asked for. Notice how value is an input register? 9, a.k.a. r9, is a perfectly valid input register. This is the assembly output I get.
li 9,4919
li 0, 9
sc
blr
If you want to load 0x1337 as an immediate, just use that instead.
asm volatile (
"li 0, 0x1337\n\t"
"sc\n\t"
"blr"
);
Or, just use the "i" constraint instead of the "r" constraint.
asm volatile (
"li 0, %0\n\t"
"sc\n\t"
"blr"
:
: "i"(0x1337)
);
来源:https://stackoverflow.com/questions/46749753/powerpc-inline-assembly-load-c-value-into-register