• Advertisement
Sign in to follow this  

Trouble converting VC inline ASM to GCC inline ASM

This topic is 4900 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I woke up 7 AM this morning after having spent a good part of the night trying to figure out why this returns an error when I try to compile it in DevC++ (using the GCC compiler). The code sample I took it from originally was for VC I believe, watching the syntax.
unsigned long CTimer::GetTicks(void)
{
    asm volatile //Taken from AMD SDK
    (
        "xor %eax, %eax;"
        "xor %ebx, %ebx;"
        "xor %ecx, %ecx;"
        "xor %edx, %edx;"
        "cpuid;"
        "rdtsc;"
        "mov %eax, (_g_Elapsed);"
        : //no output registers
        : //nothing here either
        : "%eax", "%ebx", "%ecx", "%edx"
    );
    return g_Elapsed;
}

The problem is not the code part. The problem is the fourth part, that is the "clobbed" registers' entry. If I compile without the clobbed registers I don't get any erros (in fact the code works until it is run somewhere where the destruction of EAX, EBX, ECX and EDX doesn't matter). When I try to compile it like the code above I get: "invalid `asm', C:\Dev\Rome\Code\Timer Win32.cpp:9, operand number missing after %-letter" This same error repeats itself 9 times additionally. It makes no sense since I think the syntax which I use it quite correct. Thus I have to turn to the forums for help...

Share this post


Link to post
Share on other sites
Advertisement
Never mind, I just found out what caused the error (though I don't see why). Using the % prefix for the registers is fine until they are added to the clobbered registers' list. If they are, they must be prefixed with a double %%. Example:

unsigned long CTimer::GetTicks(void)
{
asm volatile //Taken from AMD SDK
(
"xor %%eax, %%eax;"
"xor %%ebx, %%ebx;"
"xor %%ecx, %%ecx;"
"xor %%edx, %%edx;"
"cpuid;"
"rdtsc;"
"mov %%eax, (_g_Elapsed);"
: //no output registers
: //nothing here either
: "%eax", "%ebx", "%ecx", "%edx"
);

return g_Elapsed;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement