| View previous topic :: View next topic |
| Author |
Message |
Manuel
Joined: 30 Jul 2004 Posts: 4
|
Posted: Fri Jul 30, 2004 6:53 am Post subject: Inlining asm in C source |
|
|
Hello,
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
Manuel. |
|
| Back to top |
|
 |
mwolfe
Joined: 13 Jul 2004 Posts: 20
|
Posted: Fri Jul 30, 2004 8:22 am Post subject: asm("rdtsc") |
|
|
I got the following to work:
unsigned long long
get_rdtsc()
{
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 |
|
| Back to top |
|
 |
Manuel
Joined: 30 Jul 2004 Posts: 4
|
Posted: Mon Aug 02, 2004 2:04 am Post subject: Any other solution ? |
|
|
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.
regards. |
|
| Back to top |
|
 |
mwolfe
Joined: 13 Jul 2004 Posts: 20
|
Posted: Mon Aug 02, 2004 12:59 pm Post subject: |
|
|
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. |
|
| Back to top |
|
 |
Manuel
Joined: 30 Jul 2004 Posts: 4
|
Posted: Wed Aug 04, 2004 3:36 am Post subject: |
|
|
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:
| Code: | 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. |
|
| Back to top |
|
 |
|