gpt4 book ai didi

assembly - x86_64 - 为什么用 rdtsc/rdtscp 给一个程序计时会给出不合理的大数字?

转载 作者:行者123 更新时间:2023-12-05 09:36:49 32 4
gpt4 key购买 nike

我正在尝试使用 rdtscp 为子程序计时。这是我的程序:

; Setting up time
rdtscp ; Getting time
push rax ; Saving timestamp

; for(r9=0; r9<LOOP_SIZE; r9++)
mov r9, 0
lup0:
call subr
inc r9
cmp r9, LOOP_SIZE
jnz lup0

; Calculating time taken
pop rbx ; Loading old time
rdtscp ; Getting time
sub rax, rbx ; Calculating difference

如果 LOOP_SIZE 足够小,我会得到一致的预期结果。然而,当我让它足够大(大约 10^9)时,我从 10^9 飙升到 10^20。

; Result with "LOOP_SIZE equ 100000000"
971597237
; Result with "LOOP_SIZE equ 1000000000"
18446744072281657066

我用来显示数字的方法将它们显示为无符号,所以我想显示的大数字实际上是负数并且发生了溢出。但是,971597237 甚至没有接近 64 位整数限制,因此,假设问题是溢出,为什么会发生这种情况?

最佳答案

问题是 as per documentationrdtscp的值并没有存储在rax上,而是存储在edx:eax上(也就是说高位在edx上eax 上的低位),即使在 64 位模式下也是如此。

所以,如果你想在 rax 上使用完整的 64 位值,你必须从 edx 中移动更高的位:

; Setting up time
rdtscp ; Getting time
shl rdx, 32 ; Shifting rdx to the correct bit position
add rax, rdx ; Adding both to make timestamp
push rax ; Saving timestamp

; [...stuff...]

; Calculating time taken
rdtscp ; Getting time
pop rbx ; Loading old time (below rdtscp)
shl rdx, 32 ; Shifting rdx to the correct bit position
add rax, rdx ; Adding both to make timestamp
sub rax, rbx ; Calculating difference

编辑:将 pop rbx 向下移动一行,在 rdtscp 下方。正如 Peter 所指出的,某些寄存器(rax、rdx 和 rcx)可能会被 rdtscp 破坏。在您的示例中,这不是问题,但如果您决定在那里 pop rcx,那么它可能会被 rdtscp 覆盖,因此最好只弹出在它后面堆叠。


此外,您可以通过将旧时间戳保存在您的子例程不使用的寄存器中来避免对堆栈的两次调用:

; Setting up time
rdtscp ; Getting time
shl rdx, 32 ; Shifting rdx to the correct bit position
lea r12, [rdx + rax] ; Adding both to make timestamp, and saving it

; [...stuff (that doesn't use r12)...]

; Calculating time taken
rdtscp ; Getting time
shl rdx, 32 ; Shifting rdx to the correct bit position
add rax, rdx ; Adding both to make timestamp
sub rax, r12 ; Calculating difference

关于assembly - x86_64 - 为什么用 rdtsc/rdtscp 给一个程序计时会给出不合理的大数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64904820/

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