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

前端 未结 2 1022
粉色の甜心
粉色の甜心 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 00:59

    Try something like:

    #include 
    #include 
    int main(int ac,char**av)
    {
        int n=ac>1?atoi(av[1]):42;
        asm ("movl %0, %%eax \n\t"
             "neg %%eax \n\t"
             "movl %%eax, %0 \n\t" : "+r" (n)::"eax");
        printf("%d\n",n);
    }     
    

    The issues are:

    • order of operands is instr src,dst
    • %% instead of %
    • no isolated lines of assembler -- the input/output/clobber list is associated to all of the assembler block
    • '+r' to have a parameter that works as both input & output
    • I doubt even MS allows using keyword "return" that way

    And to make it even more efficient:

    asm("neg %0" : "+r" (n) ::);  // works as well
    

提交回复
热议问题