-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsomething.asm
2277 lines (2098 loc) · 41.6 KB
/
something.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; asmsyntax=nasm
bits 32
; stdcall
; The parameters are pushed onto the stack in right-to-left order.
; The callee is responsible for cleaning up the stack.
; EAX, ECX, and EDX will be modified by the callee.
; Return values are in EAX.
%macro invoke 1-*
%assign max %0 - 1
%if max > 0
%rotate max
%rep max
push %1
%rotate -1
%endrep
%endif
call %1
%endmacro
ExitProcess equ 0x00401028
GetLastError equ 0x0040102c
LoadLibraryExA equ 0x00401030
GetProcAddress equ 0x00401034
FreeLibrary equ 0x00401038
GetStdHandle equ 0x0040103c
ReadFile equ 0x00401040
WriteFile equ 0x00401044
OutputDebugStringA equ 0x00401048
HeapAlloc equ 0x0040104c
GetProcessHeap equ 0x00401050
HeapFree equ 0x00401054
STD_INPUT_HANDLE equ -10
STD_OUTPUT_HANDLE equ -11
MAX_PATH equ 256
%macro load_library 2
push 0
push 0
push %%str
push %%str_end
jmp [LoadLibraryExA]
%%str:
db %2, 0
%%str_end:
mov %1, eax
%endmacro
%macro load_function_start 1-2 1
%%start:
mov ebx, %1
mov esi, %%end + 1
mov edi, function_table_end
%%loop:
dec esi
push esi
push ebx
call [GetProcAddress]
stosd
%if %2
test eax, eax
jz %%error
%endif
push esi
call strchr0
lea esi, [eax + 1]
lodsb
test al, al
jnz %%loop
jmp esi
%if %2
%%error:
push esi
call strchr0
sub eax, esi
mov edi, [hStdOut]
push 0
push esp
push eax
push esi
push edi
call [WriteFile]
jmp exit_failure
%endif
%%end:
%endmacro
%macro load_function 1
%defstr load_function_str %1
db load_function_str, 0
%undef load_function_str
%assign %1 function_table_end
%assign function_table_end function_table_end + 4
%endmacro
%macro heap_alloc 1
push %1
push 0
call [GetProcessHeap]
push eax
call [HeapAlloc]
test eax, eax
jz exit_failure
%endmacro
absolute 0x00402000
hInst:
resd 1
hWnd:
resd 1
hKernel32:
resd 1
hUser32:
resd 1
hShell32:
resd 1
hGdi32:
resd 1
hAdvapi32:
resd 1
function_table:
%assign function_table_end $
resd 100
original_esp:
resd 1
original_text_attribute:
resd 1
hStdIn:
resd 1
hStdOut:
resd 1
argc:
resd 1
argv:
resd 1
mt:
resd 624
mti:
resd 1
conpaint_buffer:
resd 1
conpaint_buffer_width:
resd 1
conpaint_buffer_height:
resd 1
conpaint_tool:
resb 1
conpaint_ambiwidth:
resb 1
resb 2
buffer:
resb 1024
resb 0x00403000 - $
section .text
org 0x00403000
mov [original_esp], esp
push STD_INPUT_HANDLE
call [GetStdHandle]
mov [hStdIn], eax
push STD_OUTPUT_HANDLE
call [GetStdHandle]
mov [hStdOut], eax
load_library [hKernel32], "kernel32.dll"
load_library [hUser32], "user32.dll"
load_library [hShell32], "shell32.dll"
load_library [hGdi32], "gdi32.dll"
load_library [hAdvapi32], "advapi32.dll"
load_function_start [hKernel32]
load_function GetVersion
load_function AllocConsole
load_function FreeConsole
load_function SetConsoleTextAttribute
load_function SetConsoleCursorPosition
load_function GetConsoleScreenBufferInfo
load_function SetConsoleTitleW
load_function GetConsoleMode
load_function SetConsoleMode
load_function SetConsoleCursorInfo
load_function ReadConsoleInputW
load_function HeapReAlloc
load_function VirtualAlloc
load_function VirtualProtect
load_function VirtualFree
load_function SetThreadLocale
load_function GetModuleHandleW
load_function GetModuleFileNameW
load_function GetStartupInfoW
load_function GetCommandLineW
load_function WaitForSingleObject
load_function WriteConsoleOutputW
load_function SetConsoleWindowInfo
load_function FillConsoleOutputCharacterW
load_function FillConsoleOutputAttribute
db 0
load_function_start [hKernel32], 0
load_function SetThreadUILanguage
db 0
load_function_start [hUser32]
load_function MessageBoxW
load_function RegisterClassW
load_function CreateWindowExW
load_function ShowWindow
load_function UpdateWindow
load_function GetMessageW
load_function TranslateMessage
load_function DispatchMessageW
load_function LoadMenuW
load_function LoadIconW
load_function LoadCursorW
load_function LoadBitmapW
load_function LoadStringW
load_function LoadAcceleratorsW
load_function DialogBoxParamW
load_function DefWindowProcW
load_function PostQuitMessage
load_function SetTimer
load_function KillTimer
load_function SetWindowTextW
load_function BeginPaint
load_function EndPaint
load_function FillRect
db 0
load_function_start [hShell32]
load_function CommandLineToArgvW ; not available on NT 3.1
db 0
load_function_start [hGdi32]
load_function SelectObject
load_function GetStockObject
load_function SetBkMode
load_function Arc
db 0
load_function_start [hAdvapi32], 0
load_function CryptGenRandom
db 0
push 0
call [GetModuleHandleW]
mov [hInst], eax
count_commandline:
call [GetCommandLineW]
movzx edx, word [eax]
test edx, edx
jnz .nonempty_commandline
.empty_commandline:
; argc ← 1
xor eax, eax
inc eax
mov [argc], eax
; argv ← malloc(sizeof(wchar_t *) + sizeof(wchar_t) * MAX_PATH)
heap_alloc 4 + MAX_PATH * 2
mov [argv], eax
; [Ptr→][****][**\0][ ][ ]…
; EAX EDX
; argv
lea edx, [eax + 4]
mov [eax], edx
push MAX_PATH
push edx
push 0
call [GetModuleFileNameW]
jmp parse_commandline.end
.nonempty_commandline:
; Save a pointer to commandline string for use in parse_commandline.
push eax
; ESI = parsing pointer to commandline string
; ECX = number of wchar_t's
; EDX = [31] copy character?
; [30] inside quote?
; [29:0] number of arguments
; EBX = number of backslashes
mov esi, eax
xor eax, eax
mov ecx, eax
mov edx, eax
inc edx
cmp word [esi], '"'
je .quoted_program_name
.unquoted_program_name:
inc ecx
lodsw
cmp eax, ' '
ja .unquoted_program_name
test eax, eax
setz al
add eax, eax
sub esi, eax
jmp .arguments_loop_prologue
.quoted_program_name:
inc esi
inc esi
inc ecx
movzx eax, word [esi]
ror eax, 16
cmp eax, '"' << 16
sete ah
test eax, eax
setz al
or al, ah
test eax, edx ; EDX = 1
jz .quoted_program_name
mov al, ah
and eax, edx
add eax, eax
add esi, eax
.arguments_loop_prologue:
lodsw
.arguments_loop:
; eat whitespace first
cmp eax, ' '
je .arguments_loop_prologue
cmp eax, `\t`
je .arguments_loop_prologue
test eax, eax
jz .end
inc ecx
inc edx
.argument_loop:
bts edx, 31
xor ebx, ebx
; CommandLineToArgvW has a special interpretation of backslash characters when they are followed by a quotation mark character ("), as follows:
; • 2n backslashes followed by a quotation mark produce n backslashes followed by a quotation mark.
; • (2n) + 1 backslashes followed by a quotation mark again produce n backslashes followed by a quotation mark.
; • n backslashes not followed by a quotation mark simply produce n backslashes.
; — CommandLineToArgvW on MSDN
; Actually, the quotation mark following 2n backslashes toggles verbatim copying and itself is not part of the argument. Two consecutive quotation marks translate to one when in verbatim copying mode.
.eat_backslash:
cmp eax, '\'
jne .eat_quote
lodsw
inc ebx
jmp .eat_backslash
.eat_quote:
cmp eax, '"'
jne .copy_slash
; CF = 0
; CF ← EBX mod 2; EBX ← EBX / 2
rcr ebx, 1
jc .copy_slash
cmp [esi], ax ; AX = '"'
sete al
shl eax, 30
test eax, edx
setz al
stc
adc eax, eax
shl eax, 30
xor edx, eax
not eax
shr eax, 30
add esi, eax
; EAX ← [ESI − 2] = '"'
xor eax, eax
mov al, '"'
.copy_slash:
add ecx, ebx
test eax, eax
jz .arguments_loop
bt edx, 30
jc .copy_char
cmp eax, ' '
je .arguments_loop
cmp eax, `\t`
je .arguments_loop
.copy_char:
mov eax, edx
shr eax, 31
add ecx, eax
lodsw
jmp .argument_loop
.end:
and edx, 0x3fffffff
; Allocate memory for the argv array and the strings it points to.
add ecx, ecx
lea eax, [(edx + 1) * 4 + ecx]
mov ebx, edx ; save EDX = number of arguments
mov [argc], edx
heap_alloc eax
parse_commandline:
; ESI → commandline
; EDX → argv array
; EDI → string buffer
; ECX = loop count for backslashes
; BL = copy character?
; BH = inside quote?
pop esi
mov edx, eax
mov [argv], eax
lea edi, [eax + (ebx + 1) * 4]
mov [edx], edi
add edx, 4
xor eax, eax
xor ebx, ebx
cmp word [esi], '"'
je .quoted_program_name
.unquoted_program_name:
lodsw
stosw
cmp eax, ' '
ja .unquoted_program_name
test eax, eax
setz al
add eax, eax
sub esi, eax
mov [edi - 2], bx ; BX = 0
jmp .arguments_loop_prologue
.quoted_program_name:
add esi, 2
.quoted_program_name_loop:
lodsw
test eax, eax
jz .end
cmp eax, '"'
je .quoted_program_name_loop_end
stosw
jmp .quoted_program_name_loop
.quoted_program_name_loop_end:
xor eax, eax
stosw
.arguments_loop_prologue:
lodsw
.arguments_loop:
cmp eax, ' '
je .arguments_loop_prologue
cmp eax, `\t`
je .arguments_loop_prologue
test eax, eax
jz .end
mov [edx], edi
add edx, 4
.argument_loop:
mov bl, 0xff
xor ecx, ecx
.eat_backslash:
cmp eax, '\'
jne .eat_quote
lodsw
inc ecx
jmp .eat_backslash
.eat_quote:
cmp eax, '"'
jne .copy_slash
rcr ecx, 1
jc .copy_slash
cmp [esi], ax ; AX = '"'
setne bl
dec bl
and bl, bh
not bh
mov al, bl
and al, 2
add esi, eax
mov al, '"'
.copy_slash:
shl eax, 16
mov al, '\'
rep stosw
shr eax, 16
jz .append_nul
test bh, bh
jnz .copy_char
cmp eax, ' '
je .append_nul
cmp eax, `\t`
je .append_nul
.copy_char:
test bl, bl
jz .after_copy_char
stosw
.after_copy_char:
lodsw
jmp .argument_loop
.append_nul:
xor eax, eax
stosw
jmp .arguments_loop
.end:
; Append a NULL entry to argv for convenience.
xor eax, eax
mov [edx], eax
jmp main
push DEBUGBASE64STR
push buffer
call base64_atob
mov byte [buffer + eax], 0
push buffer
call [OutputDebugStringA]
push 64
push base64_table
push buffer
call base64_btoa
push buffer
call [OutputDebugStringA]
push 0
push 0
mov eax, [argv]
mov eax, [eax + 4]
push eax
push 0
call [MessageBoxW]
mov dword [mti], 0xffffffff
push 0x456
push 0x345
push 0x234
push 0x123
mov eax, esp
push 4
push eax
call init_by_array
add esp, 16
call genrand_int32
call genrand_int32
push eax
call [ExitProcess]
main:
cmp dword [argc], 2
je colorful_stub
jb conpaint
xxd:
; Loop reading characters and converting them to hexadecimal.
push 0
mov eax, esp
push 0
push eax
push 1024
push buffer
push dword [hStdIn]
call [ReadFile]
pop eax
test eax, eax
jle exit
mov ebx, buffer
lea esi, [ebx + eax]
.loop:
movzx eax, byte [ebx]
ror eax, 4
; CMP-SBB-DAS: AL 0x07 → '7'; 0x0f → 'F'
cmp al, 10
sbb al, 0x69
das
rol eax, 8
shr al, 4
cmp al, 10
sbb al, 0x69
das
xchg al, ah
or eax, `\0\0\x20`
push eax
mov eax, esp
push 0
push esp
push 3
push eax
push dword [hStdOut]
call [WriteFile]
pop eax
inc ebx
cmp ebx, esi
jb .loop
jmp xxd
conpaint:
push buffer
push dword [hStdOut]
call [GetConsoleScreenBufferInfo]
; If the cursor is at (0, 0), the application is likely to have been launched with its own console.
cmp dword [buffer + 4], 0
je .after_alloc_console
; A fresh console is wanted.
call [FreeConsole]
call [AllocConsole]
push STD_INPUT_HANDLE
call [GetStdHandle]
mov [hStdIn], eax
push STD_OUTPUT_HANDLE
call [GetStdHandle]
mov [hStdOut], eax
.after_alloc_console:
mov esi, [hStdIn]
mov edi, [hStdOut]
push console_title
call [SetConsoleTitleW]
; This sets up the console to generate keyboard, mouse and window events, and also turns insert mode off.
push 0x0098 ; ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS
push esi
call [SetConsoleMode]
push 0x0000 ; ~ENABLE_PROCESSED_OUTPUT & ~ENABLE_WRAP_AT_EOL_OUTPUT
push edi
call [SetConsoleMode]
push 1 ; visible
push 5 ; 5%
push esp
push edi
call [SetConsoleCursorInfo]
add esp, 8
event_loop:
push -1 ; INFINITE
push esi
call [WaitForSingleObject]
sub esp, 20
mov eax, esp
push 0
push esp
push 1
push eax
push esi
call [ReadConsoleInputW]
pop eax
test eax, eax
jz .epilog
; ESP → INPUT_RECORD
movzx eax, word [esp]
cmp eax, 0x0001 ; KEY_EVENT
je .key
cmp eax, 0x0002 ; MOUSE_EVENT
je .mouse
cmp eax, 0x0004 ; WINDOW_BUFFER_SIZE_EVENT
je .window
; Other types of events ought to be ignored.
jmp .epilog
.key:
push str1
call [OutputDebugStringA]
mov eax, [esp + 4]
test eax, eax
setnz cl
movzx eax, word [esp + 10]
cmp eax, 'X'
sete ch
and cl, ch
mov eax, [esp + 16]
test eax, 0x0003
setnz ch
and cl, ch
jz .key_epilog
push exit
ret 20
add esp, 20
jmp exit
.key_epilog:
call cls
jmp .epilog
.mouse:
mov edx, [esp + 16]
test edx, edx
jz .mouse_button
test edx, 0x0008 ; MOUSE_HWHEELED
test edx, 0x0004 ; MOUSE_WHEELED
jnz .mouse_wheel
test edx, 0x0001 ; MOUSE_MOVED
jnz .mouse_move
test edx, 0x0002 ; DOUBLE_CLICK
jmp .epilog
.mouse_button:
.mouse_move:
push dword [esp + 4]
push edi
call [SetConsoleCursorPosition]
mov edx, [esp + 4]
mov eax, 0x40 | ((0x9c2a) << 16)
;#define COMMON_LVB_GRID_HORIZONTAL 0x0400 // DBCS: Grid attribute: top horizontal.
;#define COMMON_LVB_GRID_LVERTICAL 0x0800 // DBCS: Grid attribute: left vertical.
;#define COMMON_LVB_GRID_RVERTICAL 0x1000 // DBCS: Grid attribute: right vertical.
;#define COMMON_LVB_REVERSE_VIDEO 0x4000 // DBCS: Reverse fore/back ground attribute.
;#define COMMON_LVB_UNDERSCORE 0x8000 // DBCS: Underscore.
; Keisen are incorporated into Windows NT for OS/2-J console applications compatibility. These attributes are designed for IBM-J’s local video display adapter’s hardware attribute sets.
; — Microsoft KB145925
or eax, [esp + 8]
mov [buffer], eax
push edx
push edx
push esp
push 0x00000000
push 0x00010001
push buffer
push edi
call [WriteConsoleOutputW]
add esp, 8
jmp .epilog
.mouse_wheel:
sub esp, 24 ; sizeof(CONSOLE_SCREEN_BUFFER_INFO) = 22
push esp
push edi
call [GetConsoleScreenBufferInfo]
movzx eax, word [esp + 12]
mov ecx, [esp + 24 + 8]
sar ecx, 16
sar ecx, 6 ; divide by 64
cmp eax, ecx
jl .mouse_wheel_epilog
sub eax, ecx
mov [esp + 12], ax
sub [esp + 16], cx
lea eax, [esp + 10]
push eax
push 1 ; absolute coordinates
push edi
call [SetConsoleWindowInfo]
.mouse_wheel_epilog:
add esp, 24
jmp .epilog
.window:
; conpaint_buffer_width ← INPUT_RECORD.Event.WindowBufferSizeEvent.dwSize.X
; conpaint_buffer_height ← INPUT_RECORD.Event.WindowBufferSizeEvent.dwSize.Y
; ECX ← .X * .Y * 4
mov edx, [esp + 4]
movzx eax, dx
shr edx, 16
mov [conpaint_buffer_width], eax
mov [conpaint_buffer_height], edx
imul eax, edx
lea ecx, [eax * 4]
; realloc(conpaint_buffer, ECX)
push ecx
mov edx, [conpaint_buffer]
test edx, edx
jz .buffer_null
push edx
push 0x00000008 ; HEAP_ZERO_MEMORY
call [GetProcessHeap]
push eax
call [HeapReAlloc]
test eax, eax
jz exit_failure
mov [conpaint_buffer], eax
jmp .epilog
.buffer_null:
push 0x00000008 ; HEAP_ZERO_MEMORY
call [GetProcessHeap]
push eax
call [HeapAlloc]
test eax, eax
jz exit_failure
mov [conpaint_buffer], eax
jmp .epilog
.epilog:
add esp, 20
jmp event_loop
; Convert internal representation to Unicode character.
; 31 24 23 16 15 12 11 8 7 0
; [0000000000] [LLBBRRTT] [IRGB] [IRGB] [ 0]
; Reserved Line sty. BG FG Type
; LL (left), BB (bottom), RR (right), TT (top): 0 = none; 1 = thin; 2 = double; 3 = thick
; BG & FG are in the same format as Windows console character attributes.
; 31 16 15 12 11 8 7 0
; [ ] [IRGB] [IRGB] [ 1]
; Unicode BMP codepoint BG FG Type
; SMP codepoints are not representable.
conpaint_atoc:
ror eax, 16
test eax, 0x00010000
movzx eax, ax
jnz .ret
mov eax, [conpaint_table + eax * 2]
.ret:
ret
colorful_stub:
push buffer
push dword [hStdOut]
call [GetConsoleScreenBufferInfo]
movzx eax, word [buffer + 8]
mov [original_text_attribute], eax
; Write a colorful stub to stdout.
mov ebx, str1
mov edi, [hStdOut]
.loop:
movzx eax, byte [ebx]
push eax
push edi
call [SetConsoleTextAttribute]
inc ebx
push 0
push esp
push 1
push ebx
push edi
call [WriteFile]
inc ebx
movzx eax, byte [ebx]
test eax, eax
jnz .loop
push `\r\n\a`
mov eax, esp
push 0
push esp
push 3
push eax
push edi
call [WriteFile]
pop eax
push dword [original_text_attribute]
push edi
call [SetConsoleTextAttribute]
winmain:
push 1041 | (0 << 16) ; MAKELCID(ja-JP, SORT_DEFAULT)
call [SetThreadLocale]
call [GetVersion]
cmp al, 6
jb .after_set_thread_ui_language
mov eax, [SetThreadUILanguage]
test eax, eax
jz .after_set_thread_ui_language
push 1041
call eax
.after_set_thread_ui_language:
push window_class_name
push 0
push 0
push 32648 ; IDC_NO
push 0
call [LoadCursorW]
push eax
push 32513 ; IDI_HAND
push 0
call [LoadIconW]
push eax
push dword [hInst]
push 0
push 0
push wndproc
push 0
push esp
call [RegisterClassW]
add esp, 40
push 0
push dword [hInst]
push 0
push 0
push 240
push 320
push 0x80000000 ; CW_USEDEFAULT
push 0x80000000 ; CW_USEDEFAULT
push 0x00cf0000 ; WS_OVERLAPPEDWINDOW
push window_title
push window_class_name
push 0x00000100 ; WS_EX_WINDOWEDGE
call [CreateWindowExW]
test eax, eax
jz exit_win32_error
mov [hWnd], eax
push 10 ; SW_SHOWDEFAULT
push eax
call [ShowWindow]
push dword [hWnd]
call [UpdateWindow]
sub esp, 28 ; sizeof(MSG)
mov esi, esp
message_loop:
push 0
push 0
push 0
push esi
call [GetMessageW]
test eax, eax
jz .end
push esi
call [TranslateMessage]
push esi
call [DispatchMessageW]
jmp message_loop
.end:
add esp, 28
jmp exit
wndproc: ; hWnd, message, wParam, lParam
push ebp
lea ebp, [esp + 8]
; Now EBP points to hWnd. This is not standard, but convenient.
mov eax, [esp + 12]
cmp eax, 0x0001 ; WM_CREATE
je .create
cmp eax, 0x0002 ; WM_DESTROY
je .destroy
cmp eax, 0x000f ; WM_PAINT
je .paint
cmp eax, 0x0113 ; WM_TIMER
je .timer
cmp eax, 0x0201 ; WM_LBUTTONDOWN
je .lbuttondown
; This is tail call.
pop ebp
jmp [DefWindowProcW]
.create:
push 0
push 33
push 114514
push dword [ebp]
call [SetTimer]
test eax, eax
jz exit_win32_error
jmp .ret
.destroy:
push 114514
push dword [ebp]
call [KillTimer]
test eax, eax
jz exit_win32_error
push 0
call [PostQuitMessage]
jmp .ret
.paint:
sub esp, 64
push esp
push dword [ebp]
call [BeginPaint]
call paint
push esp
push dword [ebp]
call [EndPaint]
add esp, 64
jmp .ret
.timer:
push 'T'
push esp
push dword [ebp]
call [SetWindowTextW]
pop eax
jmp .ret
.lbuttondown:
push 0x00000040 ; MB_INFORMATION
push 0
mov eax, esp
push 0
push eax
push 2 ; String ID = 2 "Error"
push dword [hUser32]
call [LoadStringW]
test eax, eax
jnz .after_load_string_fallback
pop eax
push window_title
.after_load_string_fallback:
push 0
mov eax, esp
push 0
push eax
push 2 ; String ID = 2
push dword [hInst]
call [LoadStringW]
test eax, eax
jz exit_win32_error