-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcrt.asm
1277 lines (1189 loc) · 20.1 KB
/
crt.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
SEGMENT CODE ; 1
; C Runtime
%include "variables.asm"
%include "func.mac"
%include "extern.inc"
%include "windows.inc"
times 16 db 0
; 10
winflags:
dw KERNEL.__WINFLAGS
; 12
loc_12:
mov al, 0xFF
push ax
call far __exit
; 1a
extern start
start:
xor bp,bp
push bp
call far KERNEL.InitTask
or ax,ax
jz short loc_12
mov [Var14BE], es
add cx, 0x100
jb short loc_12
mov [Var1484], cx
mov [crt_hPrevInstance], si
mov [crt_hInstance], di
mov [crt_lpCmdLine], bx
mov [crt_lpCmdLine+2], es
mov [crt_nCmdShow], dx
call far KERNEL.GetVersion
mov [WindowsVersion], ax
mov ah, 0x30 ; DOS3Call: Get DOS version
test word [cs:winflags], 1 ; WF_PMODE
jz .label0 ; ↓
call far KERNEL.DOS3Call
jmp short .label1 ; ↓
.label0:
int 21h
.label1:
mov [DOSVersion], ax
test word [cs:winflags], 1 ; WF_PMODE
jnz short .label2 ; ↓
mov al, 0
mov [Var14C5], al
.label2:
xor ax,ax
push ax
call far KERNEL.WaitEvent
push word [crt_hInstance]
call far USER.InitApp
or ax,ax
jz short loc_12
call far __cinit
call far __setargv
call far __setenvp
call __wcinit
push word [Var14FA]
push word [Var14F8]
push word [Var14F6]
call far __stubmain
add sp, byte 6
push ax
call far exit
; b6
__nomain:
; Never used
mov ax, 0x15 ; no main procedure
jmp __amsg_exit
; bc
extern atoi
atoi:
jmp __catox
db 0
; c0
extern atol
atol:
jmp __catox
db 0
; c4
func_nods srand
%arg seed:word ; +6
mov ax, [seed]
mov [RandomSeed], ax
mov [RandomSeed+2], word 0
endfunc
; dc
func_nods rand
mov ax, 0x43fd
mov dx, 3
push dx
push ax
push word [RandomSeed+2]
push word [RandomSeed]
call far __aFlmul
add ax, 0x9ec3
adc dx, byte 0x26
mov [RandomSeed], ax
mov [RandomSeed+2], dx
mov ax,dx
and ah, 0x7f
endfunc
; 110
extern __aFldiv
__aFldiv:
%push func
%stacksize small
%arg arg_0:dword ; 6
%arg arg_4:dword ; A
push bp
mov bp,sp
push di
push si
push bx
xor di,di
mov ax, [arg_0+2]
or ax,ax
jge short .label0 ; ↓
inc di
mov dx, [arg_0]
neg ax
neg dx
sbb ax, byte 0
mov [arg_0+2], ax
mov [arg_0], dx
.label0:
mov ax, [arg_4+2]
or ax,ax
jge short .label1 ; ↓
inc di
mov dx, [arg_4]
neg ax
neg dx
sbb ax, byte 0
mov [arg_4+2], ax
mov [arg_4], dx
.label1:
or ax,ax
jnz short .label2 ; ↓
mov cx, [arg_4]
mov ax, [arg_0+2]
xor dx,dx
div cx
mov bx,ax
mov ax, [arg_0]
div cx
mov dx,bx
jmp short .label6 ; ↓
.label2:
mov bx,ax
mov cx, [arg_4]
mov dx, [arg_0+2]
mov ax, [arg_0]
.label3:
shr bx, 1
rcr cx, 1
shr dx, 1
rcr ax, 1
or bx,bx
jnz short .label3 ; ↑
div cx
mov si,ax
mul word [arg_4+2]
xchg ax, cx
mov ax, [arg_4]
mul si
add dx,cx
jb short .label4 ; ↓
cmp dx, [arg_0+2]
ja short .label4 ; ↓
jb short .label5 ; ↓
cmp ax, [arg_0]
jbe short .label5 ; ↓
.label4:
dec si
.label5:
xor dx,dx
xchg ax, si
.label6:
dec di
jnz short .label7 ; ↓
neg dx
neg ax
sbb dx, byte 0
.label7:
pop bx
pop si
pop di
pop bp
retf 8
%pop func
; 1aa
func __stubmain
push word [crt_hInstance]
push word [crt_hPrevInstance]
push word [crt_lpCmdLine+2]
push word [crt_lpCmdLine]
push word [crt_nCmdShow]
call far WinMain
endfunc_crt
; 1d6
func __cinit
mov ax, 0x3500 ; DOS3Call: Get interrupt vector (int 0)
test word [cs:winflags], 1 ; WF_PMODE
jz short .label0 ; ↓
call far KERNEL.DOS3Call
jmp short .label1 ; ↓
.label0:
int 21h
.label1:
mov [Int0_Save], bx
mov [Int0_Save+2], es
push cs
pop ds
mov ax, 0x2500 ; DOS3Call: Set Interrupt vector (int 0)
mov dx, __cintDIV
test word [cs:winflags], 1 ; WF_PMODE
jz short .label2 ; ↓
call far KERNEL.DOS3Call
jmp short .label3 ; ↓
.label2:
int 21h
.label3:
push ss
pop ds
mov cx, [Var1518]
jcxz .label5 ; ↓
mov es, [Var14BE]
mov si, [es:0x2c]
mov ax, [Var151A]
mov dx, [Var151C]
xor bx,bx
call far [Var1516]
jnb short .label4 ; ↓
jmp __fptrap
.label4:
mov ax, [Var151E]
mov dx, [Var1520]
mov bx, 3
call far [Var1516]
.label5:
mov es, [Var14BE]
mov cx, [es:0x2c]
jcxz .label10 ; ↓
mov es, cx
xor di,di
.label6:
cmp byte [es:di], 0
jz short .label10 ; ↓
mov cx, 0xd
mov si, s_cFileInfo
repe cmpsb
jz short .label7 ; ↓
mov cx, 0x7fff
xor ax,ax
repne scasb
jnz short .label10 ; ↓
jmp short .label6 ; ↑
.label7:
push es
push ds
pop es
pop ds
mov si,di
mov di, Var14CE
mov cl, 4
.label8:
lodsb
sub al, 'A'
jb short .label9; ↓
shl al, cl
xchg ax, dx
lodsb
sub al, 'A'
jb short .label9 ; ↓
or al,dl
stosb
jmp short .label8 ; ↑
.label9:
push ss
pop ds
.label10:
mov si, NMSG
mov di, NMSG
call FUN_1_037C
mov si, NMSG
mov di, NMSG
call FUN_1_037C
mov si, NMSG
mov di, NMSG
call FUN_1_037C
endfunc_crt
; 2b5
func exit
xor cx,cx
jmp short exit_common
endstub
; 2c3
func __exit
mov cx, 1
jmp short exit_common
endstub
; 2d2
func __cexit
push si
push di
mov cx, 0x100
jmp short exit_common
endstub
; 2e3
func __c_exit
push si
push di
mov cx, 0x101
; Continue to exit_common
endstub
; 2f2
stub exit_common
%arg arg_0:word ; +6
mov [Var1502+1], ch
push cx
or cl,cl
jnz short .label0 ; ↓
mov si, Var167C
mov di, Var167C
call FUN_1_037C
mov si, NMSG
mov di, NMSG
call FUN_1_037C
mov si, [arg_0]
push si
call __wcinit
add sp, byte 2
.label0:
mov si, NMSG
mov di, NMSG
call FUN_1_037C
mov si, NMSG
mov di, NMSG
call FUN_1_037C
call __ctermsub
pop ax
or ah,ah
jnz short .label2 ; ↓
mov ax, [arg_0]
mov ah, 0x4C ; DOS3Call: Terminate program with return code
test word [cs:winflags], 1 ; WF_PMODE
jz short .label1 ; ↓
call far KERNEL.DOS3Call
jmp short .label2 ; ↓
.label1:
int 21h
.label2:
pop di
pop si
endfunc_crt
; 353
__ctermsub:
mov cx, [Var1518]
jcxz .label0 ; ↓
mov bx, 2
call far [Var1516]
.label0:
push ds
lds dx, [Int0_Save]
mov ax, 0x2500 ; DOS3Call: Set Interrupt vector (int 0)
test word [cs:winflags], 1 ; WF_PMODE
jz short .label1 ; ↓
call far KERNEL.DOS3Call
jmp short .label2 ; ↓
.label1:
int 21h
.label2:
pop ds
retn
; 37c
FUN_1_037C:
cmp si,di
jnb short .end
sub di, byte 4
mov ax, [di]
or ax, [di+2]
jz short FUN_1_037C
call far [di]
jmp short FUN_1_037C
.end:
retn
db 0
; 390
func __FF_MSGBANNER
mov ax, 0xfc
push ax
push cs
call __NMSG_WRITE
mov ax, 0xff ; run-time error
push ax
push cs
call __NMSG_WRITE
endfunc_crt
db 0
; 3b4
__setargv:
pop word [Var1508]
pop word [Var1508+2]
mov ax, 0x104
mov cx, 8
call __myalloc
mov [Var14FC+2], dx
mov [Var14FC], ax
push dx
push ax
push word [crt_hInstance]
push dx
push ax
mov ax, 0x104
push ax
call far KERNEL.GetModuleFileName
pop bx
pop es
add bx,ax
mov byte [es:bx], 0
mov dx, 1
mov di, 1
mov si, 0x81
mov ds, [Var14BE]
.label0:
lodsb
cmp al, ' '
jz short .label0 ; ↑
cmp al, 9 ; '\t'
jz short .label0 ; ↑
cmp al, 13 ; '\r'
jz short .label11 ; ↓
or al,al
jz short .label11 ; ↓
inc di
.label1:
dec si
.label2:
lodsb
cmp al, ' '
jz short .label0 ; ↑
cmp al, 9 ; '\t'
jz short .label0 ; ↑
cmp al, 13 ; '\r'
jz short .label11 ; ↓
or al,al
jz short .label11 ; ↓
cmp al, '"'
jz short .label7 ; ↓
cmp al, '\'
jz short .label3 ; ↓
inc dx
jmp short .label2 ; ↑
.label3:
xor cx,cx
.label4:
inc cx
lodsb
cmp al, '\'
jz short .label4 ; ↑
cmp al, '"'
jz short .label5 ; ↓
add dx,cx
jmp short .label1 ; ↑
.label5:
mov ax,cx
shr cx, 1
adc dx,cx
test al, 1
jnz short .label2 ; ↑
jmp short .label7 ; ↓
.label6:
dec si
.label7:
lodsb
cmp al, 13 ; '\r'
jz short .label11 ; ↓
or al,al
jz short .label11 ; ↓
cmp al, '"'
jz short .label2 ; ↑
cmp al, '\'
jz short .label8 ; ↓
inc dx
jmp short .label7 ; ↑
.label8:
xor cx,cx
.label9:
inc cx
lodsb
cmp al, '\'
jz short .label9 ; ↑
cmp al, '"'
jz short .label10 ; ↓
add dx,cx
jmp short .label6 ; ↑
.label10:
mov ax,cx
shr cx, 1
adc dx,cx
test al, 1
jnz short .label7 ; ↑
jmp short .label2 ; ↑
.label11:
push ss
pop ds
mov [Var14F6], di
add dx,di
inc di
shl di, 1
add dx,di
inc dx
and dl, 0xfe
sub sp,dx
mov ax,sp
mov [Var14F8], ax
mov bx,ax
add di,bx
push ss
pop es
lds si, [Var14FC]
mov [ss:bx], si
inc bx
inc bx
mov ds, [ss:Var14BE]
mov si, 0x81
jmp short .label13 ; ↓
.label12:
xor ax,ax
stosb
.label13:
lodsb
cmp al, ' '
jz short .label13 ; ↓
cmp al, 9 ; '\t'
jz short .label13 ; ↓
cmp al, 13 ; '\r'
jz short .label25 ; ↓
or al,al
jz short .label25 ; ↓
mov [ss:bx], di
inc bx
inc bx
.label14:
dec si
.label15:
lodsb
cmp al, ' '
jz short .label12 ; ↑
cmp al, 9 ; '\t'
jz short .label12 ; ↑
cmp al, 13 ; '\r'
jz short .label24 ; ↓
or al,al
jz short .label24 ; ↓
cmp al, '"'
jz short .label20 ; ↓
cmp al, '\'
jz short .label16 ; ↓
stosb
jmp short .label15 ; ↑
.label16:
xor cx,cx
.label17:
inc cx
lodsb
cmp al, '\'
jz short .label17 ; ↑
cmp al, '"'
jz short .label18 ; ↓
mov al, '\'
rep stosb
jmp short .label14 ; ↑
.label18:
mov al, '\'
shr cx, 1
rep stosb
jnb short .label20 ; ↓
mov al, '"'
stosb
jmp short .label15 ; ↑
.label19:
dec si
.label20:
lodsb
cmp al, 13 ; '\r'
jz short .label24 ; ↓
or al,al
jz short .label24 ; ↓
cmp al, '"'
jz short .label15 ; ↑
cmp al, '\'
jz short .label21 ; ↓
stosb
jmp short .label20 ; ↑
.label21:
xor cx,cx
.label22:
inc cx
lodsb
cmp al, '\'
jz short .label22 ; ↑
cmp al, '"'
jz short .label23 ; ↓
mov al, '\'
rep stosb
jmp short .label19 ; ↑
.label23:
mov al, '\'
shr cx, 1
rep stosb
jnb .label15 ; ↑
mov al, '"'
stosb
jmp short .label20 ; ↑
.label24:
xor ax,ax
stosb
.label25:
push ss
pop ds
mov word [bx], 0
jmp far [Var1508]
db 0
; 536
func __setenvp
push ds
call far KERNEL.GetDOSEnvironment
or ax,ax
jz short .label0 ; ↓
mov dx,ax
.label0:
mov bx,dx
mov es, dx
xor ax,ax
xor si,si
xor di,di
mov cx, -1
or bx,bx
jz short .label2 ; ↓
cmp byte [es:0], 0
jz short .label2 ; ↓
.label1:
repne scasb
inc si
scasb
jnz short .label1 ; ↑
.label2:
mov ax,di
inc ax
and al, 0xfe
inc si
mov di,si
shl si, 1
mov cx, 9
call __myalloc
push ax
mov ax,si
call __myalloc
mov [Var14FA], ax
push es
push ds
pop es
pop ds
mov cx,di
mov bx,ax
xor si,si
pop di
dec cx
jcxz .label6 ; ↓
.label3:
mov ax, [si]
cmp ax, [ss:s_cFileInfo]
jnz short .label4 ; ↓
push cx
push si
push di
mov di, s_cFileInfo
mov cx, 6
repe cmpsw
pop di
pop si
pop cx
jz short .label5 ; ↓
.label4:
mov [es:bx], di
inc bx
inc bx
.label5:
lodsb
stosb
or al,al
jnz short .label5 ; ↑
loop .label3 ; ↑
.label6:
mov [es:bx], cx
pop ds
endfunc_crt
db 0
; 5C6
__cintDIV:
push ss
pop ds
mov ax, 3 ; integer divide by 0
; Continue to __amsg_exit
; 5cb
__amsg_exit:
push ax
push ax
push cs
call __FF_MSGBANNER
push cs
call __NMSG_WRITE
push cs
call __NMSG_TEXT
xor bx,bx
or ax,ax
jz short .label1 ; ↓
mov di,ax
mov ax, 9
cmp byte [di], 'M'
jnz short .label0 ; ↓
mov ax, 0xf
.label0:
add di,ax
push di
push ds
pop es
mov al, 0xd ; '\r'
mov cx, '"'
repne scasb
mov [di-1], bl
pop ax
.label1:
push bx
push ds
push ax
call far KERNEL.FatalAppExit
mov ax, 0xff
push ax
call far KERNEL.FatalExit
db 0
; 60e
__catox:
%push func
%stacksize small
%arg arg_0:word ; +6
push bp
mov bp,sp
push di
push si
mov si, [arg_0]
xor ax,ax
cwd
xor bx,bx
.label0:
lodsb
cmp al, ' '
jz short .label0 ; ↑
cmp al, 9 ; '\t'
jz short .label0 ; ↑
push ax
cmp al, '-'
jz short .label1 ; ↓
cmp al, '+'
jnz short .label2 ; ↓
.label1:
lodsb
.label2:
cmp al, '9'
ja short .label3 ; ↓
sub al, '0'
jb short .label3 ; ↓
shl bx, 1
rcl dx, 1
mov cx,bx
mov di,dx
shl bx, 1
rcl dx, 1
shl bx, 1
rcl dx, 1
add bx,cx
adc dx,di
add bx,ax
adc dx, byte 0
jmp short .label1 ; ↑
.label3:
pop ax
cmp al, '-'
xchg ax, bx
jnz short .label4 ; ↓
neg ax
adc dx, byte 0
neg dx
.label4:
pop si
pop di
pop bp
retf
%pop func
; 662
extern __aFlmul
__aFlmul:
%push func
%stacksize small
%arg arg_0:dword ; +6
%arg arg_4:dword ; +A
push bp
mov bp,sp
mov ax, [arg_0+2]
mov cx, [arg_4+2]
or cx,ax
mov cx, [arg_4]
jnz short .label0 ; ↓
mov ax, [arg_0]
mul cx
pop bp
retf 8
.label0:
push bx
mul cx
mov bx,ax
mov ax, [arg_0]
mul word [arg_4+2]
add bx,ax
mov ax, [arg_0]
mul cx
add dx,bx
pop bx
pop bp
retf 8
%pop func
; 694
__wcinit:
push bp
mov bp,sp
pop bp
retn
; 699
__wopen:
mov ax, 0x14 ; unexpected quickwin error
jmp __amsg_exit
db 0
; 6a0
__fptrap:
mov ax, 2 ; floating point support not loaded
jmp __amsg_exit
; 6a6
func __NMSG_TEXT
%assign %$argsize 0x2
%arg arg_0:word ; +6
push si
push di
push ds
pop es
mov dx, [arg_0]
mov si, NMSG_Table
.label0:
lodsw
cmp ax,dx
jz short .label1 ; ↓
inc ax
xchg ax, si
jz short .label1 ; ↓
xchg ax, di
xor ax,ax
mov cx, -1
repne scasb
mov si,di
jmp short .label0 ; ↑
.label1:
xchg ax, si
pop di
pop si
endfunc_crt
; 6dd
func __NMSG_WRITE
%assign %$argsize 0x2
%arg arg_0:word ; +6
push di
cmp word [Var150C], byte 0
jz short .end
push word [arg_0]
push cs
call __NMSG_TEXT
or ax,ax
jz short .end
xchg ax, dx
mov di,dx
xor ax,ax
mov cx, -1
repne scasb
not cx
dec cx
mov bx, [Var14C8]
call __wopen
.end:
pop di
endfunc_crt
; 71a
__myalloc:
push bp
mov bp,sp
push bx
push es
push cx
mov cx, 0x1000
xchg cx, [Var150E]
push cx
push ax
call far __nmalloc
pop bx
pop word [Var150E]
pop cx
mov dx, ds
or ax,ax
jz short .label0 ; ↓
pop es
pop bx
jmp short .label1 ; ↓
.label0:
mov ax,cx
jmp __amsg_exit
.label1:
mov sp,bp
pop bp
retn