关于ASM的疑问

zkfly 2002-06-28 01:43:35
各位高手,我已经很有没有使用过汇编语言了,但是今天看到一段Delphi中嵌入汇编计算CPU速度的代码,疑惑了很久,不知有没有高手能帮我做个注释。谢谢。
asm
dw 310Fh //??
mov TimerLo, eax //??(TimerLo、TimerHi定义为Double)
mov TimerHi, edx //??
end;
Sleep(DelayTime); //DelayTime=500
asm
dw 310Fh //??
sub eax, TimerLo //??
sbb edx, TimerHi //??
mov TimerLo, eax //??
mov TimerHi, edx //??
end;
Result := TimerLo / (1000.0 * DelayTime) //这时候返回值是CPU当前频率
...全文
30 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
woshiwo 2002-06-29
  • 打赏
  • 举报
回复
mov TimerLo, eax //??(TimerLo、TimerHi定义为Double)为什么定义为DOUBLE
Result := TimerLo / (1000.0 * DelayTime) //这时候返回值是CPU当前频率 为什么没有TimerHi参加运算?这是一个共用体结构吗?
awinder 2002-06-28
  • 打赏
  • 举报
回复
dw 310fh
实际上就是
rdtsc指令读取一个cpu内部的64位寄存器,该寄存器每个时钟周期加一,在间隔500秒前后读取此计数器,相减后得出此段时间内cpu的时钟数,一次来计算cpu的频率。
附tdtsc指令说明:

RDTSC - Read Time-Stamp Counter
0F 31 RDTSC Read time-stamp counter into EDX:EAX

Description
Loads the current value of the processor's time-stamp counter into the EDX:EAX registers. The time-stamp counter is contained in a 64-bit MSR. The high-order 32 bits of the MSR are loaded into the EDX register, and the low-order 32 bits are loaded into the EAX register. The processor increments the time-stamp counter MSR every clock cycle and resets it to 0 whenever the processor is reset.

The time stamp disable (TSD) flag in register CR4 restricts the use of the RDTSC instruction. When the TSD flag is clear, the RDTSC instruction can be executed at any privilege level; when the flag is set, the instruction can only be executed at privilege level 0. The time-stamp counter can also be read with the RDMSR instruction, when executing at privilege level 0.

The RDTSC instruction is not a serializing instruction. Thus, it does not necessarily wait until all previous instructions have been executed before reading the counter. Similarly, subsequent instructions may begin execution before the read operation is performed.

This instruction was introduced into the Intel Architecture in the Pentium processor.

Operands Bytes Clocks
2 6-7 NP

Flags
None.
zkfly 2002-06-28
  • 打赏
  • 举报
回复
而asm.yeah.net里面说的如此的晦涩难懂,何必呢?咳~
不过还是要谢谢gwvd(鸟翔在天)
zkfly 2002-06-28
  • 打赏
  • 举报
回复
终于明白,在台湾的一个网站上找到如下信息:
“在執行了 RDTSC 指令後,現在的clock數目會存到以EDX:EAX 表示的 64 bit 值內( 高位在 EDX,低位在 EAX ),所以只要執行一個RDTSC,把 EAX 的值存起來( 不要管EDX,除非程式會跑上好幾年 )然後跑帶測的程式碼,再執行一個 RDTSC ,把新得到的 EAX 值減去舊的 EAX 值,就可以得到程式所花費的 clock 數目( 會有很小的誤差,大概是 12-20 個clock )”
而DW 310fh 实际上就是RDTSC指令。
metalbreeze 2002-06-28
  • 打赏
  • 举报
回复
asm.yeah.net有一段关于此的说明
你可以参看一下

21,459

社区成员

发帖
与我相关
我的任务
社区描述
汇编语言(Assembly Language)是任何一种用于电子计算机、微处理器、微控制器或其他可编程器件的低级语言,亦称为符号语言。
社区管理员
  • 汇编语言
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧