Trouble converting VC inline ASM to GCC inline ASM

Started by
0 comments, last by Unwise owl 19 years, 7 months ago
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...
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;}

This topic is closed to new replies.

Advertisement