Sign in to follow this  
Unwise owl

Trouble converting VC inline ASM to GCC inline ASM

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this