DalekSupreme
DalekSupreme

Reputation: 1513

asm block in c++ as a macro

I would like to write a macro which converts everything to void* even the member functions. Here is the code:

#define toVoidStar(xx,yy)\
    __asm push ecx; \
    __asm mov ecx, yy; \ 
    __asm mov [xx], ecx; \ 
    __asm pop ecx;

end it gives the error when I "call it": error C2017: illegal escape

if I use it in the code it just works fine:

void * myvoid;

__asm
{
    push ecx;
    mov ecx, imageMousePressed;
    mov [myvoid], ecx;
    pop ecx;
}

I know this is not a very nice solution but anyway can anybody help to get it work?

Upvotes: 3

Views: 2532

Answers (3)

typ1232
typ1232

Reputation: 5607

Because the __asm keyword is a statement separator, you can also put assembly instructions on the same line (see this link):

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

So the solution is to just leave out the semicolons and the compiler will know where the statements end.

Upvotes: 2

kassak
kassak

Reputation: 4194

Why not standart union hack?

template<class T>
void* as_void(T const & x)
{
   union hlp_t{
      void * v_ptr;
      T t;
   };
   hlp_t a;
   a.t = x;
   return a.v_ptr;
}

Usage: as_void(&std::vector<int>::size)

Upvotes: 8

raj raj
raj raj

Reputation: 1922

You cannot give a macro definition like:

#define toVoidStar(xx,yy)\
    __asm push ecx; \
    __asm mov ecx, yy; \ 
    __asm mov [xx], ecx; \ 
    __asm pop ecx;

since all the lines after the line __asm push ecx; will be commented out because of the ; used. See my answer for this question.

Upvotes: 2

Related Questions