Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert GCC/ATT style assembler to visual studio assembler?

Tags:

c++

gcc

assembly

I'm trying to rewrite this snippet to work in visual studio, but I clearly don't understand how to use the purpose of the colons and the meaning of __volatile__,

may you offer some help :)?

__asm__ __volatile__ (
    "mov %0, %%edi\n"
    "xor %%eax, %%eax\n"
    "xor %%ecx, %%ecx\n"
    "dec %%ecx\n"
    "repne scasb\n"
    "sub %%ecx, %%eax\n"
    "dec %%eax\n"
:
: "m" (src)
: "memory", "%ecx", "%edi", "%eax");

Thanks!

like image 363
user1326293 Avatar asked Dec 05 '25 08:12

user1326293


1 Answers

The two inline assemblers are completely different. The Visual Studio inline assembler is more primitive but easier to use. In VS:

  • inline assembly language doesn't need to be enclosed in a string;
  • instruction operands are in dest,src order;
  • C/C++ variables are accessed as is;
  • etc.

Your code doesn't have any output operators (after the first colon), so I don't see how it can have any effect. But suppose the eax register is to be saved in the src variable at the end. Then you want something like this:

char* src;
...
__asm {
    mov   edi,src
    xor   eax,eax
    xor   ecx,ecx
    dec   ecx
    repne scasb
    sub   eax,ecx
    dec   eax

    mov   src,eax    // save result
}

By the way, it looks non-optimal to me. All that business with eax could be done by not ecx, if I understand the code correctly:

__asm {
    mov   edi,src
    xor   al,al
    xor   ecx,ecx
    dec   ecx
    repne scasb
    not   ecx

    mov   src,ecx    // save result
}
like image 80
TonyK Avatar answered Dec 07 '25 22:12

TonyK