rdtsc不返回结果

问题描述 投票:1回答:1

我正在尝试将rdtsc用作计时器,但是eax和edx寄存器要么保持为空,要么形成与MS的instrin.h库的__rdtsc函数给出的数字非常不同的数字。

这里是汇编代码:

.model flat, c

.code

get_curr_cycle proc
cpuid
cpuid
cpuid
xor eax, eax ; empty eax register
xor edx, edx ; empty edx register
rdtsc
shl edx, 32 ; shift high bits to the left
or edx, eax ; or the high bits to the low bits
mov eax, edx ; move the final result into eax
retn
get_curr_cycle endp

end

这是C ++代码:

#include <iostream>
#include <intrin.h>

extern "C" long long get_curr_cycle();

int main()
{
    long long t1 = __rdtsc();
    long long t2 = get_curr_cycle();

    for(unsigned int i = 0; i <= 10; ++i)
    {
        printf("%d - %d\n", t1, t2);
    }

    getchar();

    return 0;
}

这是我的最后输出:

87592744 - 31162
87592744 - 31162
87592744 - 31162
87592744 - 31162
87592744 - 31162
87592744 - 31162
87592744 - 31162
87592744 - 31162
87592744 - 31162
87592744 - 31162
87592744 - 31162
assembly visual-c++ x86 masm rdtsc
1个回答
2
投票
根据Wikipedia

RDSTSC指令返回EDX:EAX中的TSC。在x86-64模式下,RDTSC还会清除RAX和RDX的高32位。
© www.soinside.com 2019 - 2024. All rights reserved.