gpt4 book ai didi

c - 使用 RDTSC 测量时差 - 结果太大

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:16:15 25 4
gpt4 key购买 nike

我正在尝试计算运行单个 ASM 指令所需的 CPU 周期数。为此,我创建了这个函数:

measure_register_op:
# Calculate time of required for movl operation

# function setup
pushl %ebp
movl %esp, %ebp
pushl %ebx
pushl %edi

xor %edi, %edi

# first time measurement
xorl %eax, %eax
cpuid # sync of threads
rdtsc # result in edx:eax

# we are measuring instuction below
movl %eax, %edi

# second time measurement
cpuid # sync of threads
rdtsc # result in edx:eax

# time difference
sub %eax, %edi

# move to EAX. Value of EAX is what function returns
movl %edi, %eax

# End of function
popl %edi
popl %ebx
mov %ebp, %esp
popl %ebp

ret

我在 *.c 文件中使用它:

extern unsigned int measure_register_op();

int main(void)
{

for (int a = 0; a < 10; a++)
{
printf("Instruction took %u cycles \n", measure_register_op());
}

return 0;
}

问题是:我看到的值太大了。我现在收到 3684414156。这里会出什么问题?

编辑:从 EBX 更改为 EDI,但结果仍然相似。它必须与 rdtsc 本身有关。在调试器中,我可以看到第二个测量结果为 0x7f61e078,第一个为 0x42999940,减法后仍然给出 1019758392

编辑:这是我的生成文件。也许我编译不正确:

compile: measurement.s measurement.c
gcc -g measurement.s measurement.c -o ./build/measurement -m32

编辑:这是我看到的确切结果:

Instruction took 4294966680 cycles 
Instruction took 4294966696 cycles
Instruction took 4294966688 cycles
Instruction took 4294966672 cycles
Instruction took 4294966680 cycles
Instruction took 4294966688 cycles
Instruction took 4294966688 cycles
Instruction took 4294966696 cycles
Instruction took 4294966688 cycles
Instruction took 4294966680 cycles

最佳答案

cpuid 破坏了 ebx 和许多其他寄存器。您需要避免在此处使用 cpuid 或将值保存在不会被破坏的地方。

关于c - 使用 RDTSC 测量时差 - 结果太大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56200229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com