Function __asm__ __volatile __ ("rdtsc");

I don't know what this code does exactly:

int rdtsc(){
    __asm__ __volatile__("rdtsc");

Please can someone explain to me? why rdtsc?

-5
source share
2 answers

Actually, this is not very good code.

RDTSC is the x86 instruction "ReaD TimeStamp" - it reads a 64-bit counter, which is counted in each clock cycle of your processor.

But since it is a 64-bit number, it is stored in EAX(low part) and EDX(high part), and if this code is ever used when it is embedded, I know that it EDXgets confused. At least it should be:

int rdtsc(){
    __asm__ __volatile__("rdtsc" : : "edx");

, ( "" ) EDX. :

uint64_t rdtsc()
{
   uint32_t hi, lo;
   __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
   return ( (uint64_t)lo)|( ((uint64_t)hi)<<32 );
}

, .

+5

rdtsc gcc-7 .

__builtin_ia32_rdtsc:

uint64_t tsc = __builtin_ia32_rdtsc();
+3

Source: https://habr.com/ru/post/1694212/


All Articles