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



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

屏幕的最小顯示單位是像素,像素由紅色(Red)、綠色(Green)、藍色(Blue)、Alpha等四個顏色組成,因此,在呼叫CreateWindow()時,傳入的解析度,如:320x240,代表該視窗(有效區域)的X軸有320個像素,而Y軸則是有240個像素,這個320x240像素的區域是可以用來繪製任何東西,WM_PAINT是處理視窗重新繪畫的事件,繪畫的相關處理都需要在這個事件完成,比較特別的是,Windows視窗將繪圖的許多東西抽象化,最基本的需求是:一個DC(Device Context)和一個BITMAP,DC可以想像成是一個畫台,而BITMAP則是一片畫布(Buffer),DC有了Buffer就可以畫上任何東西並將其顯示在視窗上

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
hDC         DD 0
x           DD 0
y           DD 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

ps istruc PAINTSTRUCT
    at .hdc,         DD 0
    at .fErase,      DD 0
    at .rcPaint,     DD 0, 0, 0, 0
    at .fRestore,    DD 0
    at .fIncUpdate,  DD 0
    at .rgbReserved, DB 0
iend

    section .text
WndProc:
    enter 0, 0

    cmp DWORD [ebp + ARG2], WM_PAINT
    je .paint
    cmp DWORD [ebp + ARG2], WM_CLOSE
    je .close
    cmp DWORD [ebp + ARG2], WM_DESTROY
    je .destroy
    jmp .default
   
.paint:
    push ps
    push DWORD [ebp + ARG1]
    call BeginPaint
    mov [hDC], eax
   
    xor eax, eax
    mov [y], eax
.ylp:
    xor eax, eax
    mov [x], eax
.xlp:
    push 0ffh
    push DWORD [x]
    push DWORD [y]
    push DWORD [hDC]
    call SetPixel
    inc DWORD [x]
    cmp DWORD [x], 100
    jb .xlp
    inc DWORD [y]
    cmp DWORD [y], 100
    jb .ylp
       
    push ps
    push DWORD [ebp + ARG1]
    call EndPaint
    xor eax, eax
    jmp .finish
   
.close:
    push DWORD [ebp + ARG1]
    call DestroyWindow
    xor eax, eax
    jmp .finish
        
.destroy:
    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
   
.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

完成