如何在C ++中进行内联汇编(Visual Studio 2010)
问题描述:
我正在编写一个对性能至关重要的数字运算C ++项目,其中200%的核心模块使用了70%的时间.
I'm writing a performance-critical, number-crunching C++ project where 70% of the time is used by the 200 line core module.
我想使用内联汇编来优化内核,但是我对此是全新的.但是,我确实知道一些x86汇编语言,包括GCC和NASM使用的一种.
I'd like to optimize the core using inline assembly, but I'm completely new to this. I do, however, know some x86 assembly languages including the one used by GCC and NASM.
我所知道的
我必须将汇编程序指令放在我希望它们所在的 _asm {}
中.
I have to put the assembler instructions in _asm{}
where I want them to be.
问题:
- 我不知道从哪里开始.我的嵌入式程序集开始运行时,哪个寄存器会起作用?
答
您可以按变量名访问变量并将其复制到寄存器中.这是来自MSDN的示例:
You can access variables by their name and copy them to registers. Here's an example from MSDN:
int power2( int num, int power )
{
__asm
{
mov eax, num ; Get first argument
mov ecx, power ; Get second argument
shl eax, cl ; EAX = EAX * ( 2 to the power of CL )
}
// Return with result in EAX
}
在ASM块中使用C或C ++ 对于你.