GCC asm constraints

Author: Wojciech Muła
Added on:2008-06-07

Read-write variables

asm(
        "..."
        : "+a" (var)
);

Read-only variables, registers are clobbered

This won't work, GCC complains:

asm(
        "..."
        : /* no output */
        : "a" (var)
        : "eax"
);

We can declare a temporary variable, and treat it as read-write:

int tmp_var = var;
asm(
        "..."
        : "+a" (tmp_var)
);

If there are more registers, or var shouldn't be changed, then we can declare a common dummy variable:

int dummy __attribute__((unused));
asm(
        "..."
        : "=a" (dummy),
          "=b" (dummy),
          "=c" (dummy)
        : "a" (var_or_value1),
          "b" (var_or_value2),
          "c" (var_or_value2)
);