NASM - The Netwide Assembler

NASM Forum => Example Code => Topic started by: stressful on September 22, 2016, 02:08:55 AM

Title: Simple timer for Win64
Post by: stressful on September 22, 2016, 02:08:55 AM
I have nothing to do at this hour, so here's a basic timer program for Win64. You can use this to time a chunk of your code. Try to loop it to make it worthy a timer test. If your tested code clocks 0.00000s then you probably have the fastest code on the planet. Joking  ;D

Code: [Select]
;Basic timer for Win64
;@stressful
;nasm -f win64 thisfile.asm
;golink /console /entry _main thisfile.obj msvcrt.dll kernel32.dll
global _main

section  .data
begin:   dq 0
finish:  dq 1
freq:    dq 1
million: dq 1000000.0
fmt:     db 0ah,'%.7lfs',0

section  .text
_main:
        mov     rcx,freq
        call    QueryPerformanceFrequency
        mov     rcx,begin
        call    QueryPerformanceCounter
;*********************************


        ;insert timed instructions
        ;Instructions should be long enough
        ;to be timer worthy


;*********************************
        mov     rcx,finish
        call    QueryPerformanceCounter

        mov     rax,[finish]
        mov     rbx,[begin]
        sub     rax,rbx         ;finish-begin
        fninit
        fild    qword[freq]     ;convert all to double
        fstp    qword[freq]     ;freq converted
        push    rax
        fild    qword[rsp]      ;elapsedMS converted
        fmul    qword[million]  ;elapsedMS=elapsedMS*1000000
        fld     qword[freq]
        fdivp   st1,st0         ;elapsedMS=elapsedMS/freq
        fdiv    qword[million]  ;convert ms to sec
        fstp    qword[rsp]
        pop     rdx
        mov     rcx,fmt
        call    printf
        mov     rcx,0
        call    exit


extern QueryPerformanceCounter
extern QueryPerformanceFrequency
extern exit
extern printf
extern getchar
extern _sleep   ;for verification