16

I know that rdtsc loads the current value of the processor's time-stamp counter into the two registers: EDX and EAX. In order to get it on x86 I need to do it like that (assuming using Linux):

    unsigned long lo, hi;
    asm( "rdtsc" : "=a" (lo), "=d" (hi));
    return lo;

and for x86_x64:

        unsigned long lo, hi;
        asm( "rdtsc" : "=a" (lo), "=d" (hi) ); 
        return( lo | (hi << 32) );

why is that? Can anybody explain it to me?

4

2 回答 2

9

在 x86-64 模式下,RDTSC 也会清除 RAX 的高 32 位。为了补偿这些位,我们必须将 hi 左移 32 位。

于 2013-07-01T10:36:36.840 回答
9

区别不在于rdtsc,而在于 Linux 内核想要用它做什么。

在 32 位中,它返回一个 32 位的值。所以eax中的值已经足够好了。
在 64 位中,它返回一个 64 位的值。所以它需要结合两个寄存器的值。

于 2013-07-01T11:10:41.297 回答