Inlining asm in C source


Some one here can help me ?

In a C program I try to use the assembly instruction RDTSC which ReaDs the CPU Time Stamp Counter and returns the result in EDX:EAX.

The problem is that I dont know how to transfer the values stored in the
registers EDX and EAX to C variables with inlined asm for PGCC.

I managed to develop an high precision timer for with GCC compiler
and i want to do the PGCC version :

double get_timesc()
unsigned long int edx, eax;
double x;

asm volatile (“RDTSC” : “=a”(eax) : “=d”(edx) );

x = edx*4294967296 + eax;
return (x);

How to initialise C variables from registers ?

Many thanks for your help


I got the following to work:

unsigned long long
asm (“rdtsc”);

double get_timesc()
unsigned long long s;
s = get_rdtsc();
return s;

compiled with pgcc -Masmkeyword

Ignore the pgcc warning
PGC-W-0118-Function get_rdtsc does not contain a return statement (a.c: 13)
‘rdtsc’ returns its value in %eax/%edx pair, which just happens to be the register pair used for returning long long function return values, so this works, though it’s not very pretty.
You can accumulate the result in long long unsigned integers, or in doubles, as you have been, though the long long->double conversion is another runtime function call

Thanks for your help,

I tried your solution but it does not work.

The values returned by the double function
get_timesc are not good and some times they are
also negatives.

There is any no meaning to read a register and
send its value in a C variable.


There is no way to read a register and return its value in a C register using pgcc.

I’ve not found your problems (negative return values) unless get_timesc isn’t declared extern double in the caller. I don’t have any other ideas, sorry.

Hello mwolfe,

I found a solution. In fact we can define long integer
global variables (hi_reg and lo_reg) in the C source
and then move the registers into them using the two
following asm instructions:

    mov %eax, lo_reg
    mov %edx, hi_reg

Now my timer tool has a very good precision depending
of the CPU frequence and works on AMD/Operon which was
the target machine.

Best regards.