-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwindow.asm
367 lines (289 loc) · 11.2 KB
/
window.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
; window.asm - An pure 32-bit and 64-bit win32 assembly window program
; Made by Bastiaan van der Plaat (https://bplaat.nl/)
; 32-bit: nasm -f bin window.asm -o window-x86.exe && ./window-x86
; 64-bit: nasm -DWIN64 -f bin window.asm -o window-x64.exe && ./window-x64
%include "libwindows.inc"
header
code_section
; ### Some stdlib like Win32 wrappers ###
function malloc, size
invoke GetProcessHeap
invoke HeapAlloc, _ax, 0, [size]
return
function free, ptr
invoke GetProcessHeap
invoke HeapFree, _ax, 0, [ptr]
return
function strlen, string
mov _si, [string]
mov al, [_si]
while al, "!=", 0
inc _si
mov al, [_si]
end_while
sub _si, [string]
return _si
function srand, seed
mov eax, [seed]
mov [rand_seed], eax
return
function rand
imul eax, [rand_seed], 1103515245
add eax, 12345
xor edx, edx
mov ecx, 1 << 31
idiv ecx
mov [rand_seed], edx
return _dx
; ### Window code ###
struct WindowData, \
background_color, DWORD_size
; Window procedure function
function WindowProc, hwnd, uMsg, wParam, lParam
mov eax, [uMsg]
cmp eax, WM_CREATE
je .wm_create
cmp eax, WM_SIZE
je .wm_size
cmp eax, WM_GETMINMAXINFO
je .wm_getminmaxinfo
cmp eax, WM_ERASEBKGND
je .wm_erasebkgnd
cmp eax, WM_PAINT
je .wm_paint
cmp eax, WM_DESTROY
je .wm_destroy
jmp .default
.wm_create:
local window_data, POINTER_size, \
time, SYSTEMTIME_size, \
window_rect, RECT_size, \
new_window_rect, Rect_size
; Create window data
fcall malloc, WindowData_size
mov [window_data], _ax
invoke SetWindowLongPtrA, [hwnd], GWLP_USERDATA, _ax
; Generate random seed by time
invoke GetLocalTime, addr time
movzx eax, word [time + SYSTEMTIME.wHour]
imul eax, 60
movzx ecx, word [time + SYSTEMTIME.wMinute]
add eax, ecx
imul eax, 60
movzx ecx, word [time + SYSTEMTIME.wSecond]
add eax, ecx
fcall srand, _ax
; Generate random background color
fcall rand
and eax, 0x007f7f7f
mov _di, [window_data]
mov [_di + WindowData.background_color], eax
; Center window
invoke GetClientRect, [hwnd], addr window_rect
mov eax, [window_width]
shl eax, 1
sub eax, [window_rect + RECT.right]
mov [new_window_rect + Rect.width], eax
mov eax, [window_height]
shl eax, 1
sub eax, [window_rect + RECT.bottom]
mov [new_window_rect + Rect.height], eax
invoke GetSystemMetrics, SM_CXSCREEN
sub eax, [new_window_rect + Rect.width]
shr eax, 1
mov [new_window_rect + Rect.x], eax
invoke GetSystemMetrics, SM_CYSCREEN
sub eax, [new_window_rect + Rect.height]
shr eax, 1
mov [new_window_rect + Rect.y], eax
invoke SetWindowPos, [hwnd], HWND_TOP, [new_window_rect + Rect.x], [new_window_rect + Rect.y], [new_window_rect + Rect.width], [new_window_rect + Rect.height], SWP_NOZORDER
end_local
return 0
%undef window_data
.wm_size:
; Save new window size
movzx eax, word [lParam]
mov [window_width], eax
mov eax, [lParam]
shr eax, 16
mov [window_height], eax
return 0
.wm_getminmaxinfo:
; Set window min size
mov _di, [lParam]
mov dword [_di + MINMAXINFO.ptMinTrackSize + POINT.x], 320
mov dword [_di + MINMAXINFO.ptMinTrackSize + POINT.y], 240
return 0
.wm_erasebkgnd:
; Draw no background
return TRUE
.wm_paint:
local window_data, POINTER_size, \
paint_struct, PAINTSTRUCT_size, \
hdc_buffer, POINTER_size, \
bitmap_buffer, POINTER_size, \
brush, POINTER_size, \
rect, RECT_size, \
font_size, DWORD_size, \
font, POINTER_size
; Get window data
invoke GetWindowLongPtrA, [hwnd], GWLP_USERDATA
mov [window_data], _ax
; Begin paint
invoke BeginPaint, [hwnd], addr paint_struct
; Create back buffer
invoke CreateCompatibleDC, [paint_struct + PAINTSTRUCT.hdc]
mov [hdc_buffer], _ax
invoke CreateCompatibleBitmap, [paint_struct + PAINTSTRUCT.hdc], [window_width], [window_height]
mov [bitmap_buffer], _ax
invoke SelectObject, [hdc_buffer], [bitmap_buffer]
; Draw background color
mov _si, [window_data]
invoke CreateSolidBrush, [_si + WindowData.background_color]
mov [brush], _ax
mov dword [rect + RECT.left], 0
mov dword [rect + RECT.top], 0
mov eax, [window_width]
mov [rect + RECT.right], eax
mov eax, [window_height]
mov [rect + RECT.bottom], eax
invoke FillRect, [hdc_buffer], addr rect, [brush]
invoke DeleteObject, [brush]
; Draw centered text
mov eax, [window_width]
shr eax, 4
mov [font_size], eax
invoke CreateFontA, [font_size], 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, \
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, DEFAULT_PITCH | FF_DONTCARE, font_name
mov [font], _ax
invoke SelectObject, [hdc_buffer], [font]
invoke SetBkMode, [hdc_buffer], TRANSPARENT
invoke SetTextColor, [hdc_buffer], 0x00ffffff
invoke SetTextAlign, [hdc_buffer], TA_CENTER
fcall strlen, window_title
mov esi, [window_width]
shr esi, 1
mov edi, [window_height]
sub edi, [font_size]
shr edi, 1
invoke TextOutA, [hdc_buffer], _si, _di, window_title, _ax
invoke DeleteObject, [font]
; Draw and delete back buffer
invoke BitBlt, [paint_struct + PAINTSTRUCT.hdc], 0, 0, [window_width], [window_height], [hdc_buffer], 0, 0, SRCCOPY
invoke DeleteObject, [bitmap_buffer]
invoke DeleteDC, [hdc_buffer]
; End paint
invoke EndPaint, [hwnd], addr paint_struct
end_local
return 0
.wm_destroy:
; Free window data
invoke GetWindowLongPtrA, [hwnd], GWLP_USERDATA
fcall free, _ax
; Close process
invoke PostQuitMessage, 0
return 0
.default:
invoke DefWindowProcA, [hwnd], [uMsg], [wParam], [lParam]
return
%undef hwnd
; Main entry point
entrypoint
local window_class, WNDCLASSEX_size, \
hwnd, POINTER_size, \
message, MSG_size
; Register the window class
mov dword [window_class + WNDCLASSEX.cbSize], WNDCLASSEX_size
mov dword [window_class + WNDCLASSEX.style], CS_HREDRAW | CS_VREDRAW
mov pointer [window_class + WNDCLASSEX.lpfnWndProc], WindowProc
mov dword [window_class + WNDCLASSEX.cbClsExtra], 0
mov dword [window_class + WNDCLASSEX.cbWndExtra], 0
invoke GetModuleHandleA, NULL
mov [window_class + WNDCLASSEX.hInstance], _ax
invoke LoadIconA, NULL, IDI_APPLICATION
mov [window_class + WNDCLASSEX.hIcon], _ax
mov [window_class + WNDCLASSEX.hIconSm], _ax
invoke LoadCursorA, NULL, IDC_ARROW
mov [window_class + WNDCLASSEX.hCursor], _ax
mov pointer [window_class + WNDCLASSEX.hbrBackground], NULL
mov pointer [window_class + WNDCLASSEX.lpszMenuName], NULL
mov pointer [window_class + WNDCLASSEX.lpszClassName], window_class_name
invoke RegisterClassExA, addr window_class
; Create the window
invoke CreateWindowExA, 0, window_class_name, window_title, WS_OVERLAPPEDWINDOW, \
CW_USEDEFAULT, CW_USEDEFAULT, [window_width], [window_height], \
HWND_DESKTOP, NULL, [window_class + WNDCLASSEX.hInstance], NULL
mov [hwnd], _ax
invoke ShowWindow, [hwnd], SW_SHOWDEFAULT
invoke UpdateWindow, [hwnd]
; Message loop
loop
invoke GetMessageA, addr message, NULL, 0, 0
test _ax, _ax
jle %$end_loop
invoke TranslateMessage, addr message
invoke DispatchMessageA, addr message
end_loop
invoke ExitProcess, [message + MSG.wParam]
end_local
end_code_section
data_section
; String constants
window_class_name db "window-test", 0
%ifdef WIN64
window_title db "This is a test window (64-bit)", 0
%else
window_title db "This is a test window (32-bit)", 0
%endif
font_name db "Tahoma", 0
; Global variables
rand_seed dd 0
window_width dd 800
window_height dd 600
; Import table
import_table
library gdi_table, "GDI32.DLL", \
kernel_table, "KERNEL32.DLL", \
user_table, "USER32.DLL"
import gdi_table, \
BitBlt, "BitBlt", \
CreateCompatibleBitmap, "CreateCompatibleBitmap", \
CreateCompatibleDC, "CreateCompatibleDC", \
CreateFontA, "CreateFontA", \
CreateSolidBrush, "CreateSolidBrush", \
DeleteDC, "DeleteDC", \
DeleteObject, "DeleteObject", \
SelectObject, "SelectObject", \
SetBkMode, "SetBkMode", \
SetTextAlign, "SetTextAlign", \
SetTextColor, "SetTextColor", \
TextOutA, "TextOutA"
import kernel_table, \
ExitProcess, "ExitProcess", \
GetLocalTime, "GetLocalTime", \
GetModuleHandleA, "GetModuleHandleA", \
GetProcessHeap, "GetProcessHeap", \
HeapAlloc, "HeapAlloc", \
HeapFree, "HeapFree"
import user_table, \
BeginPaint, "BeginPaint", \
CreateWindowExA, "CreateWindowExA", \
DefWindowProcA, "DefWindowProcA", \
DispatchMessageA, "DispatchMessageA", \
EndPaint, "EndPaint", \
FillRect, "FillRect", \
GetClientRect, "GetClientRect", \
GetMessageA, "GetMessageA", \
GetSystemMetrics, "GetSystemMetrics", \
GetWindowLongPtrA, GetWindowLongPtrAString, \
LoadCursorA, "LoadCursorA", \
LoadIconA, "LoadIconA", \
PostQuitMessage, "PostQuitMessage", \
RegisterClassExA, "RegisterClassExA", \
SetWindowLongPtrA, SetWindowLongPtrAString, \
SetWindowPos, "SetWindowPos", \
ShowWindow, "ShowWindow", \
TranslateMessage, "TranslateMessage", \
UpdateWindow, "UpdateWindow"
end_import_table
end_data_section