简体   繁体   中英

PowerPC Inline Assembly: Load C Value into Register

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.

9 is the register containing 0x1337 , which is exactly what you asked for. Notice how value is an input register? 9, aka 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)
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM