What does a double-percent sign (%%) do in gcc inl

2020-07-04 07:58发布

I came across a code that looks like this:

asm volatile (
    # [...]
    "movl $1200, %%ecx;"
    # [...]
);

I know what movl $1200, %ecx does in x86. but I was confused by why there are two percent signs.

标签: c gcc x86
3条回答
在下西门庆
2楼-- · 2020-07-04 08:12

It depends

  • if there is a colon : after the string, then it is an extended asm, and %% escapes the percent which could have especial meanings as mentioned by Carl. Example:

    uint32_t in = 1;
    uint32_t out = 0;
    asm volatile (
        "movl %1, %%eax;"
        "inc %%eax;"
        "movl %%eax, %0"
        : "=m" (out) /* Outputs. '=' means written to. */
        : "m" (in)   /* Inputs. No '='. */
        : "%eax"
    );
    assert(out == in + 1);
    
  • otherwise, it will be a compile time error, because without colon it is a basic asm which does not support variable constraints and does not need or support escaping %1. E.g.:

    asm volatile ("movl $1200, %ecx;");
    

    works just fine.

Extended asm is more often used since it is much more powerful.

查看更多
狗以群分
3楼-- · 2020-07-04 08:22

This helps GCC to distinguish between the operands and registers. operands have a single % as prefix. '%%' is always used with registers.

查看更多
够拽才男人
4楼-- · 2020-07-04 08:34

GCC inline assembly uses %0, %1, %2, etc. to refer to input and output operands. That means you need to use two %% for real registers.

Check this howto for great information.

查看更多
登录 后发表回答