.386
.model flat, stdcall
include win32.inc

.const

szTitleName      db 'Win32 Assembly Program', 0
szClassName      db 'ASMCLASS32',0
szMenuName	 db 0

.data

wc	WNDCLASS <?>
msg	MSGSTRUCT  <?>

.data?

newhwnd	dd ?

.code

start:
	call    GetModuleHandleA,0
	mov     [wc.clsHInstance], eax	; идентификатор программы

; подготовить и зарегистрировать класс (WNDCLASS structure)
        mov     [wc.clsStyle], CS_HREDRAW + CS_VREDRAW + CS_GLOBALCLASS
        mov     [wc.clsLpfnWndProc], offset WndProc
        mov     [wc.clsCbClsExtra], 0
        mov     [wc.clsCbWndExtra], 0

        call    LoadIconA,0,IDI_APPLICATION
        mov     [wc.clsHIcon], eax

        call    LoadCursorA, 0, IDC_ARROW
        mov     [wc.clsHCursor], eax

        mov     [wc.clsHbrBackground], COLOR_WINDOW + 1
        mov     dword ptr [wc.clsLpszMenuName], offset szMenuName
        mov     dword ptr [wc.clsLpszClassName], offset szClassName

        call    RegisterClassA, offset wc

        push    0                        ; lpParam
        push    [wc.clsHInstance]        ; hInstance
        push    0                        ; menu
        push    0                        ; parent hwnd
        push    CW_USEDEFAULT            ; height
        push    CW_USEDEFAULT            ; width
        push    CW_USEDEFAULT            ; y
        push    CW_USEDEFAULT            ; x
        push    WS_OVERLAPPEDWINDOW      ; Style
        push    offset szTitleName       ; Title string
        push    offset szClassName       ; Class name
        push    0                        ; extra style
	call    CreateWindowExA

        mov     [newhwnd], eax		; идентификатор окна
; показать окно
        call    ShowWindow,[newhwnd],SW_SHOWNORMAL
        call    UpdateWindow,[newhwnd]

;----------------- проверка сообщений от окна ------------------------
msg_loop:
	call    GetMessageA,offset msg,0,0,0
        cmp     ax, 0				; проверка на WM_QUIT
        je      end_loop

        call    TranslateMessage,offset msg
        call    DispatchMessageA,offset msg
        jmp     short msg_loop
end_loop:
        call    ExitProcess,[msg.msWPARAM]

;--------------------- минимальная процедура --------------------------

WndProc          proc uses ebx edi esi, hwnd:DWORD, wmsg:DWORD,\
                 wparam:DWORD, lparam:DWORD

        cmp     [wmsg], WM_DESTROY
        je      wmdestroy

        call    DefWindowProcA,[hwnd],[wmsg],[wparam],[lparam]
        jmp     short finish

wmdestroy:

        call    PostQuitMessage,0
        mov     eax, 0
finish:
        ret
WndProc          endp
;----------------------------------------------------------------------
ends
end start
