2

How to ask gcc to reset an asm inline input value? %0 in the following example is not reset to 42 after the first loop. So when i = 1, %0 value is still 0.

for (int i = 0; i < N; ++i)
  asm("label: substract_immediate_value %0,%0,1;"
      "compare_immediate_value %0,0;"
      "branch_not_equal label"
      : /* no outputs */
      : /* input */ "r" (42));
4

1 回答 1

1

I've not tried this, but it looks right:

for (int i = 0; i < N; ++i)
  {
    int n = 42;
    asm("label: substract_immediate_value %0,%0,1;"
        "compare_immediate_value %0,0;"
        "branch_not_equal label"
        :  "+r" (n));
  }

I presume this is a simplified example, because I can't imagine why you wouldn't just code that in C. Indeed, it's a no-op.

于 2013-04-19T09:20:27.190 回答