Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do inline assembly in C++ (Visual Studio 2010)

I'm writing a performance-critical, number-crunching C++ project where 70% of the time is used by the 200 line core module.

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.

All I know:

I have to put the assembler instructions in _asm{} where I want them to be.

Problem:

  • I have no clue where to start. What is in which register at the moment my inline assembly comes into play?
like image 462
toxic shock Avatar asked May 15 '10 10:05

toxic shock


1 Answers

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
}

Using C or C++ in ASM blocks might be also interesting for you.

like image 138
ThiefMaster Avatar answered Sep 23 '22 00:09

ThiefMaster