内联汇编的原子增量
Atomic increment with inline assembly
当我尝试以原子方式递增数字时出现此错误。 (是的,我必须使用内联汇编和命令 xaddl,而不是 fetch_and_add 等)
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);
}
如果使用数字作为约束(操作数旁边的字符串),则表示"put this operand in the same location as the one with this number"。
所以"0"(number)
意味着number
和操作数0在同一个位置。但是在这种情况下,number
是操作数0 , 所以这实际上并没有告诉编译器把它放在哪里。
您需要为 number
使用不同的约束 - 例如 "r" 如果它应该在寄存器中,或者 "m" 如果它应该在内存中。
当我尝试以原子方式递增数字时出现此错误。 (是的,我必须使用内联汇编和命令 xaddl,而不是 fetch_and_add 等)
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);
}
如果使用数字作为约束(操作数旁边的字符串),则表示"put this operand in the same location as the one with this number"。
所以"0"(number)
意味着number
和操作数0在同一个位置。但是在这种情况下,number
是操作数0 , 所以这实际上并没有告诉编译器把它放在哪里。
您需要为 number
使用不同的约束 - 例如 "r" 如果它应该在寄存器中,或者 "m" 如果它应该在内存中。