程式語言 - Netwide Assembler (NASM) - Win32 API - Set Timer



參考資訊:
https://masm32.com/board/index.php
https://www.nasm.us/xdoc/2.13rc9/html/nasmdoc0.html

Windows的Timer中斷週期是基於早期系統架構,因此,最短的Timer時間間隔為15ms,因此,即使Timer設定為1ms,觸發時間依舊為15ms

main.asm

    %include "head.asm" 

    segment .text
WndProc:
    push ebp
    mov ebp, esp
    
    cmp dword [ebp + ARG2], WM_TIMER
    je .handle_timer
    cmp dword [ebp + ARG2], WM_CLOSE
    je .handle_close
    cmp dword [ebp + ARG2], WM_DESTROY
    je .handle_destroy
    jmp .handle_default
   
.handle_timer:
    inc dword [dwCnt]
    push dword [dwCnt]
    push FMT_TIMER
    push pBuf
    call sprintf
   
    push pBuf
    push dword [ebp + ARG1]
    call SetWindowText
    xor eax, eax
    jmp .finish
   
.handle_close:
    push dword [ebp + ARG1]
    call DestroyWindow
    xor eax, eax
    jmp .finish
    
.handle_destroy:
    push 1
    push dword [ebp + ARG1]
    call KillTimer
    push 0
    call PostQuitMessage
    xor eax, eax
    jmp .finish
    
.handle_default:
    push dword [ebp + ARG4]
    push dword [ebp + ARG3]
    push dword [ebp + ARG2]
    push dword [ebp + ARG1]
    push dword [pDefWndProc]
    call CallWindowProc
    
.finish:
    leave
    ret 16
    
WinMain:
    push ebp
    mov ebp, esp
     
    push 0
    push 0
    push 0
    push 0
    push 300
    push 300
    push 0
    push 0
    push WS_OVERLAPPEDWINDOW | WS_VISIBLE
    push szAppName
    push WC_DIALOG
    push WS_EX_LEFT
    call CreateWindowEx
    mov [hWin], eax
    
    push WndProc
    push GWL_WNDPROC
    push dword [hWin]
    call SetWindowLong
    mov [pDefWndProc], eax
 
    push 0
    push 1000
    push 1
    push dword [hWin]
    call SetTimer
     
.loop:
    push 0
    push 0
    push 0
    push msg
    call GetMessage
    cmp eax, 0
    je .exit
     
    push msg
    call DispatchMessage
    jmp .loop
     
.exit:
    mov eax, [msg + MSG.wParam]
    leave
    ret 16
     
_start:
    push 0
    call GetModuleHandle
    mov [hInstance], eax
     
    call GetCommandLine
    mov [pCommand], eax
     
    push SW_SHOWNORMAL
    push dword [pCommand]
    push 0
    push dword [hInstance]
    call WinMain
     
    push eax
    call ExitProcess

Line 16~27:累加數值並且顯示在視窗標題
Line 36~38:關閉Timer
Line 81~85:設定Timer為每秒(1000ms)觸發一次,ID=1

完成