简体   繁体   中英

Atomic increment with inline assembly

I get this error, when I try to atomically increment number. (Yes, I have to do it with inline assembly and command xaddl, and not fetch_and_add etc.)

tslock.c:23:3: error: matching constraint references invalid operand number
   : "cc", "memory");
   ^
tslock.c:20:2: error: matching constraint references invalid operand number
  __asm__ __volatile__ (
  ^

void atomicIncrement(int number){
    int one = 1;
    __asm__ __volatile__ (
            "lock xaddl %1, %0;"        
        :: "0"(number),  "m"(one)
        : "cc", "memory");

    printf("new value = %d\n", number);

}

If you use a number as a constraint (the string next to the operand), it means "put this operand in the same location as the one with this number".

So "0"(number) means that number goes in the same location as operand 0. But in this case, number is operand 0, so that doesn't actually tell the compiler where to put it.

You need to use a different constraint for number - for example "r" if it should be in a register or "m" if it should be in memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM