What does a double-percent sign (%%) do in gcc inline assembly?

前端 未结 3 1874
既然无缘
既然无缘 2021-01-04 11:17

I came across a code that looks like this:

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

I know what movl $120

3条回答
  •  孤城傲影
    2021-01-04 11:43

    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.

提交回复
热议问题