我试图使用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发布于 2020-05-17 23:36:28
根据维基百科
指令RDTSC返回EDX:EAX中的TSC。在x86-64模式下,RDTSC还清除较高的32位RAX和RDX.
因此,在x86上,您的代码可以是:
get_curr_cycle proc
rdtsc
retn
get_curr_cycle endp它将返回edx:eax中的当前计时器值。
在x64上,您可以这样做:
get_curr_cycle proc
rdtsc
shl rdx, 32
or rax, rdx
retn
get_curr_cycle endp这将返回rax中的计时器值。
此外,您的printf格式说明符也是错误的。他们应该是%lld。
https://stackoverflow.com/questions/61860126
复制相似问题