[英]How to use asm code in C++ & Visual Studio 2008
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. 我正在做一些跨平台的工作,将代码从Linux移植到Win32,但是当涉及到一些汇编代码时,我无法使其工作。 This code compiles normally in Linux gcc, but will not work in VS2008 --win32.
该代码在Linux gcc中正常编译,但在VS2008 --win32中不起作用。
Some of my code is below. 我的一些代码如下。 What do I need to do if I want to compile it using VS2008 --win32 platform:
如果要使用VS2008 --win32平台进行编译,该怎么办:
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");
A thing like "spin_wait" isn't portable, and should be (re)written to what's appropriate for the target platform. 诸如“ spin_wait”之类的内容不可移植,应(重新)写入适合目标平台的内容。 It's been a while since I've dealt with the horrible GAS syntax, but xorq means the snippet is for x86-64, right?
自从我处理了可怕的GAS语法以来已经有一段时间了,但是xorq意味着该代码段适用于x86-64,对吧? The Microsoft compilers don't support inline assembly for 64bit targets.
Microsoft编译器不支持64位目标的内联汇编。
Will _AcquireSpinLock perhaps do the trick? _AcquireSpinLock可能会成功吗?
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). 对于较大的汇编块(通常可以用汇编编写的部分),我建议您将外部汇编模块与具有合理语法和良好功能集的跨平台汇编器一起使用(例如yasm,fasm或nasm)。 Of course that's not viable for something as critical and must-be-inline as a spinlock, though :)
当然,这对于像自旋锁这样的关键且必须内联的东西并不可行,但:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.