Domanda

So I'm trying compile some C in GCC for windows. Long story short I can't get Visual Studios to compile an EXE that works on XP. So I thought I'd give GCC a try.

The code it's struggling with is:

__asm __volatile ("rdtsc": "=a" (lower), "=d"(upper));

And the error I'm getting is:

HITWxp.c:22:2: error: inconsistent operand constraints in an 'asm'
__asm __volatile ("rdtsc": "=A" (lower), "=D"(upper));
^

Now it compiles when I change the line to this:

__volatile ("rdtsc": "=A" (lower));

I have noticed its converting the "=a" from the first example to the capital "=A" in the second example. So I figured that it's not case sensitive.

The end result needs to be and EXE that works on WinXP/7/8/8.1 x86/x64.

Any ideas?

Thanks in advance!

È stato utile?

Soluzione

Works fine on MingW32 and CygWin:

#include <stdio.h>
#include <stdint.h>

int main ( void )
{
    uint32_t lower = 0, upper = 0;

    asm volatile ("rdtsc": "=a" (lower), "=d" (upper));

    uint64_t ull = ((uint64_t) upper << 32) + lower;
    printf ("%08X %08X\n", upper, lower);
    printf ("%llu\n", ull);
    return 0;
}

and Visual Studio 2010:

#include <stdio.h>

int main ( void )
{
    unsigned _int64 ull = 0;

    _asm
    {
        rdtsc
        mov dword ptr ull, eax
        mov dword ptr ull+4, edx
    }

    printf("%I64u\n",ull);
    return 0;
}

and this one should work on both Visual Studios, x86 and x64:

#include <stdio.h>
#include <intrin.h>    

int main ( void )
{
    unsigned _int64 ull = 0;

    ull = __rdtsc();    // "Intrinsics"

    printf("%I64u\n",ull);
    return 0;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top