How handling the Pentium cycle counter QNX4?

Hi Community !!!

I use following Assambler Code with Auxilliary Pragmas and all works
perfect, but I dont understand exactly what happens.


#pragma aux rdtsc64=
“db 0Fh,31h”
“mov [ebx],eax”
“mov [ebx+4],edx”
parm nomemory[ebx] modify exact nomemory[eax edx]


Is annybody able to explain me exactly what happens ??

Thank you !!!

Code: 0F 31
Mnemonic: RDTSC
Description: Loads the current value of the processor’s time-stamp counter
into the EDX:EAX registers. The time-stamp counter is contained
in a 64-bit MSR. The high-order 32 bits of the MSR are loaded
into the EDX register, and the low-order 32 bits are loaded into
the EAX register. The processor increments the time-stamp counter
MSR every clock cycle and resets it to 0 whenever the processor
is reset.
The time stamp disable (TSD) flag in register CR4 restricts the
use of the RDTSC instruction. When the TSD flag is clear, the
RDTSC instruction can be executed at any privilege level; when the
flag is set, the instruction can only be executed at privilege level 0.
The time-stamp counter can also be read with the RDMSR instruction,
when executing at privilege level 0. The RDTSC instruction is not a
serializing instruction. Thus, it does not necessarily wait until all
previous instructions have been executed before reading the counter.
Similarly, subsequent instructions may begin execution before the read
operation is performed.
This instruction was introduced into the Intel Architecture in the
Pentium processor.

Thank You lestat !!!

and what happens in


parm nomemory[ebx] modify exact nomemory[eax edx]


nomemory means that operations with ebx and eax, edx registers doesn’t touch memory, only register-to-register copies. Really this code is bad - it doing write to memory with ebx as index.

parm - means that argument is ebx.

modify means that eax and ebx may be or not may be modified and compiler anyway need to save content of that registers.

exact means that eax and edx registers will be exactly modified. Used to help compiler with analyzing.

For example with Watcom 11 you can do the following without memory access:

unsigned __int64 rdtsc( void);
#pragma aux rdtsc = “db 0x0F 0x31” value [edx eax] parm nomemory modify exact [edx eax] nomemory;

and use it just as:

__int64 tsc=rdtsc();

Great!!!

Thank you very much for your describe in detail lestat!!!

And why is that bad?

Code itself is not bad. It’s bad that it uses nomemory [ebx] construction when it is really writes to memory. This potentially can bedevil the compiler.