首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rdtsc不返回结果。

rdtsc不返回结果。
EN

Stack Overflow用户
提问于 2020-05-17 23:27:57
回答 1查看 171关注 0票数 0

我试图使用rdtsc作为计时器,但是eax和edx寄存器要么保持为空,要么形成一个与MS的instrin.h库中的__rdtsc函数所提供的数字非常不同的数字。

以下是组装代码:

代码语言:javascript
复制
.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++代码:

代码语言:javascript
复制
#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;
}

这是我的最后一个输出:

代码语言:javascript
复制
87592744 - 31162
87592744 - 31162
87592744 - 31162
87592744 - 31162
87592744 - 31162
87592744 - 31162
87592744 - 31162
87592744 - 31162
87592744 - 31162
87592744 - 31162
87592744 - 31162
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-17 23:36:28

根据维基百科

指令RDTSC返回EDX:EAX中的TSC。在x86-64模式下,RDTSC还清除较高的32位RAX和RDX.

因此,在x86上,您的代码可以是:

代码语言:javascript
复制
get_curr_cycle proc
rdtsc
retn
get_curr_cycle endp

它将返回edx:eax中的当前计时器值。

在x64上,您可以这样做:

代码语言:javascript
复制
get_curr_cycle proc
rdtsc
shl rdx, 32
or rax, rdx
retn
get_curr_cycle endp

这将返回rax中的计时器值。

此外,您的printf格式说明符也是错误的。他们应该是%lld

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61860126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档