rdtsc64.asm 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. %include "defs.asm"
  2. ; RDTSC64.ASM
  3. ;
  4. ; Author: Agner Fog
  5. ; Date created: 2003
  6. ; Last modified: 2008-10-16
  7. ; Description:
  8. ;
  9. ; Copyright (c) 2009 GNU General Public License www.gnu.org/licenses
  10. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  11. default rel
  12. global ReadTSC: function
  13. SECTION .text align=16
  14. ; ********** ReadTSC function **********
  15. ; C++ prototype:
  16. ; extern "C" __int64 ReadTSC (void);
  17. ; This function returns the value of the time stamp counter, which counts
  18. ; clock cycles. To count how many clock cycles a piece of code takes, call
  19. ; Rdtsc before and after the code to measure and calculate the difference.
  20. ; The number of clock cycles taken by the ReadTSC function itself is approximately:
  21. ; Core 2: 730
  22. ; Pentium 4: 700
  23. ; Pentium II and Pentium III: 225
  24. ; AMD Athlon 64, Opteron: 126
  25. ; Does not work on 80386 and 80486.
  26. ; Note that clock counts may not be fully reproducible on Intel Core and
  27. ; Core 2 processors because the clock frequency can change. More reliable
  28. ; instruction timings are obtained with the performance monitor counter
  29. ; for "core clock cycles". This requires a kernel mode driver as the one
  30. ; included with www.agner.org/optimize/testp.zip.
  31. ReadTSC:
  32. push rbx ; ebx is modified by cpuid
  33. sub eax, eax ; 0
  34. cpuid ; serialize
  35. rdtsc ; read time stamp counter into edx:eax
  36. shl rdx, 32
  37. or rax, rdx ; combine into 64 bit register
  38. push rax
  39. sub eax, eax
  40. cpuid ; serialize
  41. pop rax ; return value
  42. pop rbx
  43. ret
  44. ;ReadTSC ENDP