Original Post
Hi, I'm doing some work with lock-free, thread-safe data structures, for which I need to use some inline assembly language. I'm using template wrapper functions for this so my code isn't quite so messy. The following is what I use to access the x86's cmpxchg instruction on GCC, for 32-bit integers or pointers: This works exactly how I want it to. The problem is that is uses named operands (%[newv], %[mem], %[flag]), which aren't supported by Intel's C++ compiler, although all other aspects of the GCC inline assembly language syntax seem to be supported. (this is ICC 8.1 for Linux by the way) The Question: How could I modify above code snippet to work correctly with numbered (%0, %1, %2) operands? The problem lies with the implicit use of register %eax, which isn't explicitly specified in the assembly instruction. I obviously don't have a good enough understanding of how the %0, %1, %2, etc. placeholders are matched against the input and output operand list. Thanks ~phil [Edited by - JonnyQuest on February 26, 2006 6:53:57 AM]
template <typename T> inline static bool compare_and_exchange(volatile T& dest, T& old_val_out, T old_val, T new_val)
{
bool result;
__asm__ __volatile__(
"lock cmpxchgl %[newv], %[mem] \n setzb %[flag]"
: [flag] "=qm"(result), [mem] "+m" (dest), "=a" (old_val_out)
: "a" (old_val), [newv] "r" (new_val)
: );
return result;
}