extended asm in gcc: ‘asm’ operand has impossible constraints

后端 未结 1 1000
借酒劲吻你
借酒劲吻你 2021-01-14 03:07

This function \"strcpy\" aims to copy the content of src to dest, and it works out just fine: display two lines of \"Hello_src\".

#include          


        
1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-14 03:40

    The earlyclobber & means that the particular output is written before the inputs are consumed. As such, the compiler may not allocate any input to the same register. Apparently using the 0/1 style overrides that behavior.

    Of course the clobber list also has important use. The compiler does not parse your assembly code. It needs the clobber list to figure out which registers your code will modify. You'd better not lie, or subtle bugs may creep in. If you want to see its effect, try to trick the compiler into using a register around your asm block:

    extern int foo();
    int bar()
    {
        int x = foo();
        asm("nop" ::: "eax");
        return x;
    }
    

    Relevant part of the generated assembly code:

    call    foo
    movl    %eax, %edx
    nop
    movl    %edx, %eax
    

    Notice how the compiler had to save the return value from foo into edx because it believed that eax will be modified. Normally it would just leave it in eax, since that's where it will be needed later. Here you can imagine what would happen if your asm code did modify eax without telling the compiler: the return value would be overwritten.

    0 讨论(0)
提交回复
热议问题