; Listing5-6.asm
;
; A program that demonstrates a Windows Timer Object.

            option     casemap:none
            include    aoalib.inc   ;AoA library + constants
            includelib aoalib.lib   ;Link in aoalib library
            include    c:\masm32\include64\win64.inc
            include    c:\masm32\include64\kernel32.inc
            includelib c:\masm32\lib64\kernel32.lib  
            
            .const

; Program title:

            align   word
ttlStr      byte    "Listing 5-6", 0

            .data
            align   qword 

; TimeValue is in 100 nsec increments.
           
TimeValue   qword   -2*1000*1000*1000/100   ;2 seconds from now

; Period is in milliseconds

Period      qword   2000                    ;2 seconds

; Handle for the timer

hTimer      qword   ?

            .code


threadProc    proc
              local  threadVar1:dword, threadVar2:dword
              local  threadVar3:qword, threadStr[64]:byte
	  mov	al, threadStr[2]
              ret  
threadProc    endp
        
; Here is the main assembly language function.

            
            public  asmMain
asmMain     proc
            
            push    rbp
            mov     rbp, rsp
            sub     rsp, 64
            mov     [rbp-8], rbx
            
; Create a periodic timer:

            xor     rcx, rcx                ;Default security attributes
            xor     rdx, rdx                ;Synchronization timer
            xor     r8, r8                  ;NULL timer name
            call    CreateWaitableTimer
            test    eax, eax                ;Check for error
            jz      badTimer
            mov     hTimer, rax             ;Save handle
            
            mov     rcx, rax                ;hTimer handle
            lea     rdx, TimeValue          ;lpDueTime
            mov     r8, Period              ;lPeriod
            xor     r9, r9                  ;No callback function
            mov     [rsp+32], r9            ;NULL parameter
            mov     [rsp+40], r9            ;Don't resume on pwr down
            call    SetWaitableTimer
            test    eax, eax
            jz      badTimer
            
            mov     rdx, rax
            call    print
            byte    "SetWait: %d", nl, 0 

            mov     ebx, 10                 ;Repeat the loop 10 times
rptLoop:    mov     rcx, hTimer             ;Wait for timer event
            mov     edx, INFINITE
            call    WaitForSingleObject
            
            mov     edx, 10
            sub     edx, ebx
            call    print
            byte    "Time: %d", nl, 0
            
            dec     ebx
            jnz     rptLoop 
            
            
            mov     rcx, hTimer             ;Turn off the timer
            call    CancelWaitableTimer
            
            mov     rcx, hTimer             ;Free the timer
            call    CloseHandle
            jmp     allDone
            
badTimer:   call    GetLastError
            mov     edx, eax
            call    print
            byte    "Bad timer operation, error: %d", nl, 0
            
; Terminate the program.

allDone:    mov     rbx, [rbp-8]
            leave
            ret     ;Returns to caller
        
asmMain     endp
            end