Skip to content

Instantly share code, notes, and snippets.

@lostkagamine
Created May 12, 2021 19:26
Show Gist options
  • Save lostkagamine/46f11ad5b0cc37c7795cca1630f4e511 to your computer and use it in GitHub Desktop.
Save lostkagamine/46f11ad5b0cc37c7795cca1630f4e511 to your computer and use it in GitHub Desktop.
.386
.model flat, stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\msvcrt.inc
include \masm32\include\gdi32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\gdi32.lib
includelib \masm32\lib\msvcrt.lib
.data
class db "ExampleWinClass", 0
wintitle db "This is a window", 0
ourtext db "Hello, world! 223 lines of pure x86 assembly <3", 0
crWhite COLORREF 00FFFFFFh
.data?
hinst HINSTANCE ?
cmdl LPSTR ?
.code
entrypoint proc
push NULL
call GetModuleHandle
mov hinst, eax
call GetCommandLineA
mov cmdl, eax
push hinst
push SW_SHOWDEFAULT
call WinMain
xor eax, eax
ret
entrypoint endp
WinMain proc hInstance:HINSTANCE, show:DWORD
LOCAL wc:WNDCLASSEX
LOCAL msg:MSG
LOCAL hwnd:HWND
mov wc.cbSize, SIZEOF WNDCLASSEX
mov eax, hInstance
mov wc.hInstance, eax
mov wc.style, CS_HREDRAW or CS_VREDRAW
lea eax, class
mov wc.lpszClassName, eax
mov wc.lpfnWndProc, OFFSET WndProc
push IDI_WINLOGO
push NULL
call LoadIcon
mov wc.hIcon, eax
push IDC_ARROW
push NULL
call LoadCursor
mov wc.hCursor, eax
lea eax, wc
push eax
call RegisterClassEx ; Register our window class
push NULL
push hInstance
push NULL
push NULL
push 480
push 640
push CW_USEDEFAULT
push CW_USEDEFAULT
push WS_OVERLAPPEDWINDOW
lea eax, wintitle
push eax
lea eax, class
push eax
push 0
call CreateWindowExA
mov hwnd, eax
push hwnd
call UpdateWindow
push SW_NORMAL
push hwnd
call ShowWindow
msgloop:
push 0
push 0
push NULL
lea eax, msg
push eax
call GetMessage
cmp eax, 0
je quit
lea eax, msg
push eax
call TranslateMessage
lea eax, msg
push eax
call DispatchMessage
jmp msgloop
quit:
xor eax, eax ; Return 0
ret
WinMain endp
WndProc proc hWnd:HWND, uMsg:UINT, wparam:WPARAM, lparam:LPARAM
cmp uMsg, WM_DESTROY
je WmDestroy
cmp uMsg, WM_PAINT
je WmPaint
push lparam
push wparam
push uMsg
push hWnd
call DefWindowProc
ret
WmPaint:
push lparam
push wparam
push hWnd
call PaintProc
xor eax, eax
ret
WmDestroy:
push 0
call PostQuitMessage
xor eax, eax
ret
WndProc endp
PaintProc proc hWnd:HWND, wparam:WPARAM, lparam:LPARAM
local ps:PAINTSTRUCT
local hdc:HDC
local cr:RECT
local brush:HBRUSH
lea eax, ps
push eax
mov eax, hWnd
push eax
call BeginPaint
mov hdc, eax
lea eax, cr
push eax
push hWnd
call GetClientRect
push TRANSPARENT
push hdc
call SetBkMode
push crWhite
call CreateSolidBrush
mov brush, eax
push brush
lea eax, cr
push eax
push hdc
call FillRect
xor eax, eax
push DT_VCENTER or DT_CENTER or DT_SINGLELINE
lea eax, cr
push eax
push -1
lea eax, ourtext
push eax
push hdc
call DrawText
xor eax, eax
push brush
call DeleteObject
lea eax, ps
push eax
mov eax, hWnd
push eax
call EndPaint
xor eax, eax
ret
PaintProc endp
end entrypoint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment