format PE64 GUI 6.0
entry sec_start

include 'win64w.inc'

section '.idata' import data readable writeable
  library kernel32,'KERNEL32.DLL',\
          user32,  'USER32.DLL'
	
  import kernel32,\
         GetModuleHandle, 'GetModuleHandleA',\
         ExitProcess, 'ExitProcess'
  import user32,\
         RegisterClassEx, 'RegisterClassExA',\
         CreateWindowEx, 'CreateWindowExA',\
         DefWindowProc, 'DefWindowProcA',\
         GetMessage, 'GetMessageA',\
				 TranslateMessage, 'TranslateMessage',\
         DispatchMessage, 'DispatchMessageA',\
				 ShowWindow, 'ShowWindow',\
				 PostQuitMessage, 'PostQuitMessage',\
				 LoadIcon, 'LoadIconA',\
				 LoadCursor, 'LoadCursorA'

section '.data' data readable writeable
  s_ClassTitle TCHAR "My program's window title", 0 ; Set window title and class name
	
	wc WNDCLASSEX ?
	msg MSG ?
	
	l_hWnd dq ?
	l_hInstance dq ?

section '.text' code readable writeable executable

	sec_start:
		invoke GetModuleHandle, NULL
		mov [l_hInstance], rax
		mov [wc.hInstance], rax
		
		mov [wc.cbSize], sizeof.WNDCLASSEX
		mov [wc.style], CS_HREDRAW or CS_VREDRAW
		mov [wc.lpfnWndProc], procWnd
		mov [wc.cbClsExtra], 0
		
		invoke LoadIcon, 0, IDI_APPLICATION
		mov [wc.hIcon], rax
		mov [wc.hIconSm], rax
		
		invoke LoadCursor, 0, IDC_ARROW
		mov [wc.hCursor], rax
		
		mov [wc.hbrBackground], COLOR_WINDOW+1
		mov [wc.lpszMenuName], NULL
		mov [wc.lpszClassName], s_ClassTitle
		
		invoke RegisterClassEx, wc
		invoke CreateWindowEx, 0, s_ClassTitle, s_ClassTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, [l_hInstance], NULL
		mov [l_hWnd], rax
		invoke ShowWindow, [l_hWnd], SW_SHOW

	sec_msg_loop:
		invoke GetMessage, msg, NULL, 0, 0
		or rax, rax
		je sec_end_prog
		
		invoke TranslateMessage, msg
		invoke DispatchMessage, msg
		jmp sec_msg_loop
	
	sec_end_prog:
		invoke ExitProcess, [msg.wParam]
	
	proc procWnd hWnd, msg, wParam, lParam
		push rbx rsi rdi
		cmp [msg], WM_DESTROY
		je sec_destroy_wnd
		
		invoke DefWindowProc, [hWnd], [msg], [wParam], [lParam]
		jmp sec_finish
		
		sec_destroy_wnd:
			invoke PostQuitMessage,0
			
		sec_finish:
			pop rbx rsi rdi
			ret
endp