我试图将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
According to Wikipedia:
因此,在x86上,您的代码可以简单地是:
Which will return the current timer value in
edx:eax
.在x64上,您可以执行以下操作:
This will return the timer value in
rax
.Also, your format specifiers for
printf
are wrong. They should be%lld
.