Search code examples
armclanginline-assemblyarm64

value size does not match register size specified by the constraint and modifier


In arm64, compiling the inline assembly below causes a warning below:

value size does not match register size specified by the constraint and modifier [-Wasm-operand-widths].

This is because the arm64's register is 64bit.

int a = 1;
int b = 2;
asm volatile(
   "add %[a], %[a], %[b]  \n"
   :
   [a] "+r"(a)
   :
   [b] "r"(b)
   :
    );

And clang give the fix advice, use constraint modifier "w".


Solution

  • value size does not match register size specified by the constraint and modifier

    means:

    your variable is 32bit not match with ARM64's 64bit register

    you could:

    • change your variable to 64bit
    long a = 1;
    long b = 2;
    asm volatile(
       "add %[a], %[a], %[b]  \n"
       : [a] "+r"(a)
       : [b] "r"(b)
       :);
    

    or

    • use 32bit=Word register, by add prefix w
      • which is clang suggested
    int a = 1;
    int b = 2;
    asm volatile(
       "add %w[a], %w[a], %w[b]  \n"
       : [a] "+r"(a)
       : [b] "r"(b)
       :);