简体   繁体   English

如何在C ++和Visual Studio 2008中使用asm代码

[英]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");

If your question is about the use of inline assembly, you can still use it in Visual Studio ( MSDN link ) although you may need to tweak it for the platform of course. 如果您的问题是关于内联汇编的使用,尽管您可能需要针对平台进行微调,但仍然可以在Visual Studio中使用它( MSDN链接 )。

For example: 例如:

__asm {
   mov al, 2
   mov dx, 0xD007
   out dx, al
}

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.

相关问题 Visual Studio C ++:看到ASM代码? - Visual Studio C++: Seeing the ASM code? 如何在VIsual Studio C ++ 2008中使用本地时间功能 - how to use localtime function with VIsual Studio C++ 2008 如何将Visual Studio 2010与Visual C ++ 2008编译器一起使用? - How can I use Visual Studio 2010 with the Visual C++ 2008 compiler? 静态代码分析器:非托管C ++ Visual Studio 2008 - Static-code analyzer: unmanaged C++ Visual Studio 2008 C ++代码在CentOS g ++中编译,但在Visual Studio 2008中不编译 - C++ code compiles in CentOS g++, but not in Visual studio 2008 如何使用Visual Studio 2008打开vcxproj(Visual C ++项目) - How to open vcxproj (visual c++ project) with visual studio 2008 Visual Studio 2008(或更高版本)使用哪种C ++? - Which C++ does Visual Studio 2008 (or later) use? 如何在 Visual Studio Code 的 C++ 项目中使用外部库 - How to use external libraries in a C++ project in Visual Studio Code 如何使用Visual Studio 2008确定C ++代码的复杂度(如CPU周期) - How to determine complexity (as in CPU cycles) of C++ code with Visual Studio 2008 ASM 在 c++ 项目中......这个小 asm 代码将如何在 c++ 中 - ASM In c++ project …How this little asm code will be in c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM