0

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!

user3078629
  • 139
  • 1
  • 2
  • 9
  • What version of gcc are you using? – Krum Apr 24 '14 at 16:41
  • 4.8.1 I did think this. If it's due to that how do I get the new version in Windows? Because GCC does produce an EXE that works in XP – user3078629 Apr 24 '14 at 16:42
  • Or how would I compile it in Linux for Windows. Because I understand by splitting the line that is producing the error produces the RDTSC of both x86 and x64 OS's. So I kinda need that one :) – user3078629 Apr 24 '14 at 16:45
  • what type are you using for lower and upper? – Krum Apr 24 '14 at 16:45

1 Answers1

0

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;
}
rkhb
  • 14,159
  • 7
  • 32
  • 60