Reputation: 1439
I need to get instruction encoding for atomic increment of a long variable. I was thinking of writing some inline gcc assembly and use the gdb disassembled output to get the answer. Here is what I did.
#include <stdint.h>
int lock_inc(uint64_t *value) {
__asm__ __volatile__
(
"lock inc %0;\n"
:
: "r"(value)
: "memory"
);
}
uint64_t value = 0;
int main() {
lock_inc(&value);
}
But when I try to compile it I get the following error.
lockinc.c: Assembler messages: lockinc.c:5: Error: expecting lockable instruction after `lock'
What am I doing wrong? By the way if somebody know the direct answer (encoding) that would be great as well. (I was referring to some sites for getting it directly but wasn't sure of the answer. Felt letting gcc do it would be easier.)
Upvotes: 0
Views: 365
Reputation: 7483
You can't use lock on registers. You must use "m" for the constraint, not "r". Also, since you are both reading and writing, "value" should be an output marked with "+". And why not just use __sync_add_and_fetch?
Upvotes: 1