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

前端 未结 3 1875
既然无缘
既然无缘 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:37

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-04 11:53

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

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