error: invalid 'asm': operand number missing after %-letter when using inline assembly with GCC

前端 未结 2 1029
粉色の甜心
粉色の甜心 2020-12-12 00:25

I\'m tring to convert a simple assembly code of MS to use with gcc, the MS assembly I try to convert is right below. I have two int variables, number

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 01:04

    You can't do it like that because you're overwriting registers without telling the compiler about it. Also, the % is a special character, similar to printf.

    It's also better to put all the instructions in one asm or else the compiler might do something unexpected in between.

    Try this instead:

    asm("movl %%eax, %1\n\t"
        "neg %%eax\n\t"
        "movl %0, %%eax" : "=g" ( _return )  : "g" ( number) : "eax");
    

    There's probably a better way, though:

    asm("neg %0": "=a" ( _return )  : "a" ( number));
    

    I don't know why you can't just do (in C):

    _return = -number;
    

提交回复
热议问题