Reputation: 37
I am doing some cross-platform work, porting code from Linux to Win32, but when it comes to some assembly code, I cannot get it to work. This code compiles normally in Linux gcc, but will not work in VS2008 --win32.
Some of my code is below. What do I need to do if I want to compile it using VS2008 --win32 platform:
static inline void spin_wait(int n)
{
int tmp = n;
while(tmp > 0) { tmp--; asm("" ::: "memory", "cc"); }
}
__asm__ __volatile__(
"xorq %%rdx, %%rdx \n"
"xorq %%rax, %%rax \n"
"incq %%rdx \n"
"lock cmpxchgq %%rdx, (%1) \n"
"decq %%rax \n"
: "=a" (not_set) : "r" (n) : "%rdx");
Upvotes: 0
Views: 1529
Reputation: 5018
A thing like "spin_wait" isn't portable, and should be (re)written to what's appropriate for the target platform. It's been a while since I've dealt with the horrible GAS syntax, but xorq means the snippet is for x86-64, right? The Microsoft compilers don't support inline assembly for 64bit targets.
Will _AcquireSpinLock perhaps do the trick?
For larger blocks of assembly (the parts that generally make sense to write in assembly), I'd advise you to use an external assembly module with a cross-platform assembler with sane syntax and a good feature set (for instance yasm, fasm or nasm). Of course that's not viable for something as critical and must-be-inline as a spinlock, though :)
Upvotes: 1