程式語言 - 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 "/opt/wadk/nasm/inc/windows.inc"
 
    section .data
hWin        DD 0
pCmdLine    DD 0
hInstance   DD 0
pDefWndProc DD 0
szAppName   DB "main",0
pBuf        times 255 DB 0
dwCnt       DD 0
FMT_TIMER   DB "%d",0

msg istruc MSG
    at .hwnd,    DD 0
    at .message, DD 0
    at .wParam,  DD 0
    at .lParam,  DD 0
    at .time,    DD 0
    at .pt,      DD 0, 0
iend

    section .text
WndProc:
    enter 0, 0
     
    cmp DWORD [ebp + ARG2], WM_TIMER
    je .timer
    cmp DWORD [ebp + ARG2], WM_CLOSE
    je .close
    cmp DWORD [ebp + ARG2], WM_DESTROY
    je .destroy
    jmp .default
    
.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
    
.close:
    push DWORD [ebp + ARG1]
    call DestroyWindow
    xor eax, eax
    jmp .finish
     
.destroy:
    push 1
    push DWORD [ebp + ARG1]
    call KillTimer
    push 0
    call PostQuitMessage
    xor eax, eax
    jmp .finish
     
.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:
    enter 0, 0
      
    push NULL
    push NULL
    push NULL
    push NULL
    push 240
    push 320
    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 [pCmdLine], eax
      
    push SW_SHOWNORMAL
    push DWORD [pCmdLine]
    push 0
    push DWORD [hInstance]
    call WinMain
      
    push 0
    call ExitProcess

完成