PowerPC Inline Assembly: Load C Value into Register

大憨熊 提交于 2019-12-11 16:59:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!