Skip to main content
GameDev.net gamedev.net
🔒 Locked

[RESOLVED] GCC, Intel C++ Inline Assembler: instruction operand placeholders

Started by JonnyQuest Feb 25, 2006 at 11:10 AM 0 replies 3.2k views
Original Post
JonnyQuest
JonnyQuest
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:

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;
}



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]
~phil
JonnyQuest
JonnyQuest
OK, I've figured it out. Numbered operands aren't so hard after all. :-D

For future reference, the solution is as follows:

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 %4, %1 \n setzb %0"		: "=qm"(result), "+m" (dest), "=a" (old_val_out)		: "a" (old_val), "r" (new_val));	return result;}


Also note that I've removed the empty 'clobbers' list, as Intel C++ didn't like that.

~phil
~phil

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.