-
Notifications
You must be signed in to change notification settings - Fork 3
/
os.asm
2346 lines (2133 loc) · 52.4 KB
/
os.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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; OS.SYS - Implements minimal support for the ;;
;; DOS syscalls that SASM needs to run. ;;
;; ;;
;; Copyright 2019 Michael Rasmussen ;;
;; See LICENSE.md for details ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
cpu 8086
org 0x0500
; TODO: Should probably switch to O/S stack when processing syscalls
; TODO: Always push all registers in the interrupt handler and modify
; AX/flags there (or wherever the return value goes) rather than
; doing for each syscall handler.
SECTOR_SIZE equ 512
BPB_SECSPERCLUST equ 0x02 ; BYTE Logical sectors per cluster
BPB_RSRVEDSECS equ 0x03 ; WORD Number of reserved logical sectors
BPB_NUMFATS equ 0x05 ; BYTE Number of File Allocation Tables
BPB_MAXROOTENTS equ 0x06 ; WORD Maximum number of root directory entries
BPB_TOTALSECTORS equ 0x08 ; WORD Total number of sectors on disk
BPB_SECSPERFAT equ 0x0B ; WORD Logical sectors per FAT
BPB_SECPERTRACK equ 0x0D ; WORD Physical sectors per track
BPB_NUMHEADS equ 0x0F ; WORD Number of heads
BPB_SIZE equ 0x13
BPB_OFFSET equ 0x7C0B
DIR_ENTRY_FNAME equ 0x00 ; BYTE[8] File name
DIR_ENTRY_EXT equ 0x08 ; BYTE[3] Extension
DIR_ENTRY_ATTR equ 0x0B ; BYTE File attributes
DIR_ENTRY_FTIME equ 0x16 ; WORD File time
DIR_ENTRY_FDATE equ 0x18 ; WORD File date
DIR_ENTRY_LCLUST equ 0x1A ; WORD Low word of start cluster (high word only used for FAT32)
DIR_ENTRY_FSIZE equ 0x1C ; DWORD File size
DIR_ENTRY_SIZE equ 0x20
ENTRY_DELETED equ 0xE5 ; Special value of DIR_ENTRY_FNAME[0]
EOC_CLUSTER equ 0xFFF ; End of cluster chain marker (other values might be present on disk)
MAX_FILES equ 8
FILE_INFO_FOFF equ 0 ; DWORD Current file offset
FILE_INFO_BUFSEG equ 4 ; WORD Buffer segment
FILE_INFO_BUFOFF equ 6 ; WORD Offset into buffer
FILE_INFO_BUFSZ equ 8 ; WORD Number of valid bytes in buffer
FILE_INFO_CLUST equ 10 ; WORD Next cluster to get / Last cluster written
FILE_INFO_DIRENT equ 12 ; WORD Offset into RootSeg of file entry
FILE_INFO_MODE equ 14 ; BYTE Mode (0 = Closed, 1 = Open for reading, 2 = Open for writing)
FILE_INFO_PSP equ 15 ; WORD Owning process PSP
FILE_INFO_SIZE equ 17 ; Size of file info
; Legal values for FILE_INFO_MODE
FMODE_CLOSED equ 0
FMODE_READ_ONLY equ 1
FMODE_WRITE_ONLY equ 2
; Find First offsets into DTA
FF_STEMPLATE equ 0x01 ; BYTE[11] Search template
FF_SATTR equ 0x0C ; BYTE Search attribute mask
FF_ROOT_IDX equ 0x0D ; WORD Current index (offset) into RootSeg
FF_FATTR equ 0x15 ; BYTE File attribute
FF_FTIME equ 0x16 ; WORD File time
FF_FDATE equ 0x18 ; WORD File date
FF_FSIZE equ 0x1A ; DWORD File size
FF_FNAME equ 0x1E ; BYTE[13] File name and extension (ASCIIZ with dot)
; Program Segment Prefix offsets
PSP_INT20 equ 0x00 ; WORD int 0x20 instruction
PSP_MAXSEG equ 0x02 ; WORD Segment of first byte beyond memory allocated to program
PSP_OLDINT22 equ 0x0A ; DWORD Old Int22 (Termination handler)
PSP_OLDINT23 equ 0x0E ; DWORD Old Int23 (Ctrl+C handler)
PSP_OLDINT24 equ 0x12 ; DWORD Old Int24 (Critical error handler)
PSP_PARENTPSP equ 0x16 ; WORD Parent PSP segment
PSP_OLDSP equ 0x42 ; WORD Old SP (0x42-0x4F are reserved)
PSP_OLDSS equ 0x44 ; WORD Old SS
PSP_CMDLEN equ 0x80 ; BYTE Number of byte in command line
PSP_CMDDAT equ 0x81 ; BYTE[0x7F] Command Line
PSP_SIZE equ 0x100
; DOS error codes
ERR_NONE equ 0x00 ; No error
ERR_FUNC_INV equ 0x01 ; Function number invalid
ERR_FILE_NOT_FND equ 0x02 ; File not found
ERR_PATH_NOT_FND equ 0x03 ; Path not found
ERR_TOO_MANY_FIL equ 0x04 ; Too many open files
ERR_ACCESS_DENIE equ 0x05 ; Access denied
ERR_INVALID_HAND equ 0x06 ; Invalid handle
ERR_NO_MEM equ 0x08 ; Insufficient memory
ERR_NO_FILES equ 0x12 ; No more files
NOT_FOUND equ 0xFFFF ; Returned when entry/pointer not found
INT20_OFF equ 0x20*4
INT20_SEG equ 0x20*4+2
INT21_OFF equ 0x21*4
INT21_SEG equ 0x21*4+2
INT22_OFF equ 0x22*4
INT22_SEG equ 0x22*4+2
Main:
; Save boot drive (passed by boot loader)
mov [BootDrive], dl
; Move stack to free up unused memory
cli
mov ax, ProgramEnd
add ax, 512 ; O/S stack size
mov sp, ax
sti
add ax, 15
mov cl, 4
shr ax, cl
mov [FreeSeg], ax
; Get free memory
int 0x12
mov cl, 6 ; log2(1024/16)
shl ax, cl
mov [MaxSeg], ax
; Add dot to loading message
mov al, '.'
call PutChar
; Copy BPB from boot sector
push ds
pop es
xor ax, ax
mov ds, ax
mov di, BPB
mov si, BPB_OFFSET
mov cx, BPB_SIZE
rep movsb
push cs
pop ds
; Set intterupt vectors
xor ax, ax
mov word [INT20_OFF], Int20Dispatch
mov word [INT20_SEG], ax
mov word [INT21_OFF], Int21Dispatch
mov word [INT21_SEG], ax
mov word [INT22_OFF], DefaultTerminate
mov word [INT22_SEG], ax
mov ax, SECTOR_SIZE
mov bx, [BPB+BPB_SECSPERFAT]
call Malloc
mov [FATSeg], ax
xor dx, dx
xor ah, ah
mov al, [BPB+BPB_SECSPERCLUST]
mov bx, SECTOR_SIZE
mul bx
mov [ClusterBytes], ax
mov ax, [BPB+BPB_MAXROOTENTS]
mov cl, 5
shl ax, cl
mov [RootMaxIdx], ax
call GetFatRootSec
mov dx, ax
call GetFatNRootSecs
add ax, dx
mov [DataSector], ax
mov ax, [BPB+BPB_TOTALSECTORS]
sub ax, [DataSector]
xor dx, dx
xor bh, bh
mov bl, [BPB+BPB_SECSPERCLUST]
div bx
add ax, 2 ; +2 since (valid) data clusters are offset by 2
mov [MaxCluster], ax
mov ax, MAX_FILES
mov bx, FILE_INFO_SIZE
call MallocBytes
mov [FileInfoSeg], ax
mov es, ax
mov cl, MAX_FILES
xor bx, bx
.InitFile:
xor ax, ax
mov [es:bx+FILE_INFO_FOFF], ax
mov [es:bx+FILE_INFO_FOFF+2], ax
mov [es:bx+FILE_INFO_BUFOFF], ax
mov [es:bx+FILE_INFO_BUFSZ], ax
mov [es:bx+FILE_INFO_CLUST], ax
mov word [es:bx+FILE_INFO_DIRENT], NOT_FOUND
mov [es:bx+FILE_INFO_MODE], al
mov [es:bx+FILE_INFO_PSP], ax
push es
push bx
push cx
mov ax, [ClusterBytes]
mov bx, 1
call MallocBytes
pop cx
pop bx
pop es
mov [es:bx+FILE_INFO_BUFSEG], ax
add bx, FILE_INFO_SIZE
dec cl
jnz .InitFile
; Read FAT
xor di, di
mov ax, [FATSeg]
mov es, ax
mov ax, [BPB+BPB_RSRVEDSECS]
mov cx, [BPB+BPB_SECSPERFAT]
call ReadSectors
call GetFatNRootSecs
mov bx, SECTOR_SIZE
call MallocBytes
mov [RootSeg], ax
; Add dot to loading message
mov al, '.'
call PutChar
; Read root directory
mov ax, [RootSeg]
mov es, ax
xor di, di
call GetFatNRootSecs
mov cx, ax
call GetFatRootSec
call ReadSectors
call PutCrLf ; Terminate Loading... message
; Load command interpreter
mov dx, CmdpFName
call ExpandFName
call LoadProgram
and ax, ax
jnz .LoadOK
mov bx, MsgErrCmdNotF
jmp short Fatal
.LoadOK:
mov [CmdpSeg], ax
; Start command interpreter
call StartProgram ; Shouldn't return
mov bx, MsgErrCmdpRet
; Fall through
; Halt with error message in BX
Fatal:
push cs
pop ds
push bx
mov bx, cs
mov ds, bx
mov bx, MsgErrFatal
call PutString
pop bx
call PutString
call PutCrLf
mov bx, MsgReboot
call PutString
xor ax, ax
int 0x16
int 0x19
PutCrLf:
mov al, 13
call PutChar
mov al, 10
; Fall through
; Print character in AL
; Preserves all normal registers
PutChar:
push ax
push bx
push bp ; Some BIOSes destroy BP when the screen scrolls
mov bx, 7
mov ah, 0x0e
int 0x10
pop bp
pop bx
pop ax
ret
; Print string in BX
PutString:
mov al, [bx]
and al, al
jnz .P
ret
.P:
call PutChar
inc bx
jmp PutString
%if 0
; Print word in AX
PutHexWord:
push ax
mov al, ah
call PutHexByte
pop ax
%endif
PutHexByte:
push ax
shr al, 1
shr al, 1
shr al, 1
shr al, 1
call PutHexDigit
pop ax
PutHexDigit:
and al, 0x0f
add al, '0'
cmp al, '9'
jbe PutChar
add al, 7
jmp PutChar
; Convert LBA in AX to CHS in CX/DX
LBAtoCHS:
push ax
xor dx, dx
div word [BPB+BPB_SECPERTRACK]
inc dl
mov cl, dl ; CL=sector
xor dx, dx
div word [BPB+BPB_NUMHEADS]
mov dh, dl ; DH=head
mov dl, [BootDrive] ; DL=drive number
mov ch, al ; CH=cylinder
pop ax
ret
; AX = FAT_RES_SECS + FAT_NUM_FATS * FAT_SEC_CNT
GetFatRootSec:
push dx
xor dx, dx
xor ah, ah
mov al, [cs:BPB+BPB_NUMFATS]
mul word [cs:BPB+BPB_SECSPERFAT]
add ax, [cs:BPB+BPB_RSRVEDSECS]
pop dx
ret
; AX = FAT_MAX_ROOTS
GetFatNRootSecs:
mov ax, [cs:BPB+BPB_MAXROOTENTS]
shr ax, 1
shr ax, 1
shr ax, 1
shr ax, 1
ret
; Adjust cluster in AX to match LBA and set CX to sector count
; AX = FAT_ROOT_SEC + FAT_MAX_ROOTS - 2
GetClusterParams:
xor ch, ch
mov cl, [cs:BPB+BPB_SECSPERCLUST]
sub ax, 2
push dx
xor dx, dx
mul cx
add ax, [cs:DataSector]
pop dx
; CX = SectorsPerCluster
; AX = DATA_SECTOR + (cluster - 2) * SectorsPerCluster
ret
; Read cluster in AX to ES:DI
ReadCluster:
call ClusterValid
jnc .ClusterValid
jmp InvalidCluster
.ClusterValid:
call GetClusterParams
; Fall through
; Read CX sectors starting from AX into ES:DI
ReadSectors:
push ax
push bx
push cx
push dx
push es
.Read:
push cx
call LBAtoCHS
push ax
mov bx, di
mov ax, 0x0201
int 0x13
jc .DiskReadErr
pop ax
pop cx
mov bx, es
add bx, SECTOR_SIZE/0x10
mov es, bx
inc ax
dec cx
jnz .Read
pop es
pop dx
pop cx
pop bx
pop ax
ret
.DiskReadErr:
mov al, ah
call PutHexByte
mov al, ' '
call PutChar
mov bx, MsgErrRead
jmp Fatal
; Write cluster in AX from ES:DI
; See also ReadCluster
WriteCluster:
call ClusterValid
jnc .ClusterValid
jmp InvalidCluster
.ClusterValid:
call GetClusterParams
; Fall through
; Write CX sectors starting from AX from ES:DI
WriteSectors:
push ds
push es
push ax
push bx
push cx
push dx
mov dx, cs
mov ds, dx
.Write:
push cx
call LBAtoCHS
push ax
mov bx, di
mov ax, 0x0301
int 0x13
jc .DiskWriteErr
pop ax
pop cx
mov bx, es
add bx, SECTOR_SIZE/0x10
mov es, bx
inc ax
dec cx
jnz .Write
pop dx
pop cx
pop bx
pop ax
pop es
pop ds
ret
.DiskWriteErr:
mov al, ah
call PutHexByte
mov al, ' '
call PutChar
mov bx, MsgErrWrite
jmp Fatal
; Alloc AX*BX bytes (Must be < 64K)
MallocBytes:
mul bx
and dx, dx
jnz MallocOOM
mov cl, 4
shr ax, cl
; Fall through
; Alloc AX paragraphs, returns segment in AX
Malloc:
mov bx, [FreeSeg]
add ax, bx
cmp ax, [MaxSeg]
ja MallocOOM
mov [FreeSeg], ax
mov ax, bx
ret
MallocOOM:
mov bx, MsgErrOOM
jmp Fatal
; Load program from CurFileName
; Returns AX=Loaded segment (0 on error)
; Assumes CS=DS
LoadProgram:
call FindFileInRoot
cmp bx, NOT_FOUND
jne .Found
xor ax, ax
ret
.Found:
push bx
push es
mov ax, [MaxSeg]
sub ax, [FreeSeg]
cmp ax, 0x20
jb MallocOOM
mov bx, 0x1000
cmp ax, bx
jbe .SizeLimited
mov ax, bx
.SizeLimited:
mov bp, ax
sub bp, PSP_SIZE/0x10 + 1 ; Need room for PSP and a little stack
mov cl, 4
shl bp, cl ; BP = Maximum bytes to read
mov bx, ax
shl bx, cl
sub bx, 4 ; Make room for 2 words on stack (see below)
mov [ChildSSSP], bx
; Allocate room
call Malloc
mov [ChildSSSP+2], ax
mov word [ChildCSIP], 0x0100
mov [ChildCSIP+2], ax
; Build PSP (TODO: More...)
mov es, ax
mov word [es:PSP_INT20], 0x20CD ; Int 20h
mov bx, [MaxSeg]
mov word [es:PSP_MAXSEG], bx ; Segment of first byte beyond memory allocated by program
mov byte [es:PSP_CMDLEN], 0 ; No command line arguments
mov bx, [CurrentPSP]
mov [es:PSP_PARENTPSP], bx ; Store parent PSP
; Store old interrupt vectors
mov si, INT22_OFF
mov di, PSP_OLDINT22
mov cx, 3*2
rep movsw
; Ensure there's a 0 at the bottom of the stack
; So a local return will execute the INT 20 instruction at [cs:0]
; And the initial value of AX
mov di, [ChildSSSP]
xor ax, ax
stosw ; initial AX=0 is more compatible
stosw
; Point DTA at PSP:80h
mov [DTA+2], es
mov word [DTA], PSP_CMDLEN
; Update CurrentPSP
mov [CurrentPSP], es
pop es
pop bx
; Open file and read
call OpenFileFromDE
mov bx, ax
push bx
push ds
mov ds, [CurrentPSP]
mov dx, PSP_SIZE
mov cx, bp
call ReadFile
jnc .ReadOK
.ReadErr:
mov bx, MsgErrRead
jmp Fatal
.ReadOK:
cmp ax, bp ; Read maximum bytes?
jb .ReadDone
mov cx, 1 ; Check that we're at EOF
call ReadFile
jc .ReadErr
and ax, ax
jz .ReadDone
mov bx, MsgErrProgLoad ; File not completely read
jmp Fatal
.ReadDone:
pop ds
pop bx
; AX = bytes read
; Close again
call CloseFileHandle
; Return with segment in ax
mov ax, [CurrentPSP]
ret
; Start loaded program at segment AX
StartProgram:
cli
mov ds, ax
; Save stack pointer
mov [PSP_OLDSP], sp
mov [PSP_OLDSS], ss
; Match some of the register values (see http://www.fysnet.net/yourhelp.htm)
mov ss, [cs:ChildSSSP+2]
mov es, ax
mov dx, [cs:ChildCSIP+2]
mov sp, [cs:ChildSSSP]
mov cx, 0x00FF
mov si, [cs:ChildCSIP]
mov di, sp
mov bp, 0x0900
pop bx ; Get initial value of AX
mov ax, 0x0202 ; Interrupts enabled
push ax ; Push flags
xor ax, ax
xchg ax, bx
push dx
push si
iret
ret
; Return carry clear if cluster index in AX is valid
ClusterValid:
cmp ax, [cs:MaxCluster]
jae .Invalid
cmp ax, 2
jb .Invalid
clc
ret
.Invalid:
stc
ret
; Return next cluster after AX in AX
NextCluster:
call ClusterValid
jc InvalidCluster
mov bx, [FATSeg]
mov es, bx
mov bx, ax
add bx, bx
add bx, ax
shr bx, 1
mov ax, [es:bx]
jnc .Even
mov cl, 4
shr ax, cl
.Even:
and ah, 0x0f
ret
InvalidCluster:
mov bx, MsgErrCluster
jmp Fatal
; Add new cluster to chain in AX
; Returns new cluster in AX (or 0 if none could be found)
AddCluster:
push si
mov si, ax ; Save previous cluster in SI
mov es, [cs:FATSeg]
; First find free cluster
xor bx, bx ; FAT pointer
xor cx, cx ; cluster
.Search:
mov dx, [es:bx] ; DX=H0L2L1L0
mov ax, dx ; AX=H0L2L1L0
and ax, 0xfff ; AX=__L2L1L0
jz .Found
inc cx
add bx, 2
mov dl, dh ; DX=____H0L2
mov dh, [es:bx] ; DX=H2H1H0L2
shr dx, 1 ; DX=__H2H1H0
shr dx, 1
shr dx, 1
shr dx, 1
jz .Found
inc cx
add bx, 1
cmp cx, [cs:MaxCluster]
jb .Search
; None found - disk is full
xor ax, ax
jmp short .Ret
.Found:
push cx
mov ax, si
call ClusterValid
jc .Done
; Update last cluster
mov bx, ax
mov ax, cx
call UpdateCluster
.Done:
pop ax
push ax
mov bx, ax
mov ax, EOC_CLUSTER
call UpdateCluster
pop ax
.Ret:
pop si
ret
; Update cluster in BX to point to AX
; Assumes ES=FatSeg
UpdateCluster:
and ax, 0x0FFF ; Ensure we're not messing with bits we shouldn't be
mov cx, bx
add bx, bx
add bx, cx
shr bx, 1
mov cx, [es:bx]
jnc .Even
shl ax, 1
shl ax, 1
shl ax, 1
shl ax, 1
and cl, 0x0F
or al, cl
jmp short .UCDone
.Even:
and ch, 0xF0
or ah, ch
.UCDone:
mov [es:bx], ax
ret
; Free cluster chain in AX
FreeClusterChain:
call ClusterValid
jnc .Free
ret
.Free:
push ax
call NextCluster
pop bx
push ax
xor ax, ax ; Mark as free
call UpdateCluster
pop ax
jmp FreeClusterChain
; Compress filename in DS:SI to ES:DI
CompressFName:
push si
push di
push si
mov cl, 8
.Name:
lodsb
cmp al, ' '
je .NameDone
stosb
dec cl
jnz .Name
.NameDone:
mov al, '.'
stosb
pop si
add si, 8
mov cl, 3
.Ext:
lodsb
cmp al, ' '
je .ExtDone
stosb
dec cl
jnz .Ext
.ExtDone:
xor al, al
stosb
pop di
pop si
ret
; Expand filename in DS:DX to CurFileName
ExpandFName:
push di
mov ax, cs
mov es, ax
mov di, CurFileName
mov bx, dx
xor cx, cx
.FName:
mov al, [bx]
cmp al, '.'
je .Dot
and al, al
jz .FillRest
call .Store
jmp .FName
.Dot:
inc bx
.Fill1:
cmp cl, 8
je .Ext
mov al, ' '
stosb
inc cl
jmp .Fill1
.Ext:
mov al, [bx]
and al, al
jz .FillRest
call .Store
jmp .Ext
.FillRest:
cmp cl, 11
jae .FillDone
mov al, ' '
stosb
inc cl
jmp .FillRest
.FillDone:
; If the first character is 0xE5 replace it with 0x05
cmp byte [es:CurFileName], ENTRY_DELETED
jne .ND
mov byte [es:CurFileName], 0x05
.ND:
pop di
ret
.Store:
cmp al, 'a'
jb .R
cmp al, 'z'
ja .R
and al, 0xDF ; to upper case
.R:
stosb
inc bx
inc cl
ret
; Expand file specification in DS:DX to Find file template in DTA (ES:BX)
; Follows the algorithm from https://devblogs.microsoft.com/oldnewthing/20071217-00/?p=24143
; Preserves DS:DX and ES:BX
ExpandFSpec:
push bx
push si
push di
mov si, dx
add bx, FF_STEMPLATE
; 1. Start with 11 spaces and the cursor at position 1
mov di, bx
mov cx, 11
mov al, ' '
rep stosb
mov di, bx ; Cursor at position 1
.ReadChar:
; 2. Read a character from input and stop if at end
lodsb
and al, al
jz .Stop
; 3. If the next character is a dot then set extension to ' ',
; mov the cursor to position 9 and go to step 2
cmp al, '.'
jne .CheckAsterisk
mov word [es:bx+8], ' '
mov byte [es:bx+10], ' '
mov di, bx
add di, 8
jmp .ReadChar
.CheckAsterisk:
; 4. If the next character is an asterisk, then fill the rest of
; the pattern with question marks and move the cursor past the end
; and go to step 2
cmp al, '*'
jne .NotAsterisk
mov cx, bx
sub cx, di
add cx, 11
mov al, '?'
rep stosb
mov di, bx
add di, 11
jmp .ReadChar
.NotAsterisk:
; 5. If not past the end, copy the character, and go to step 2
mov cx, di
sub cx, bx
cmp cl, 11
jae .ReadChar
; ToUpper
cmp al, 'a'
jb .Store
cmp al, 'z'
ja .Store
and al, 0xDF
.Store:
stosb
jmp .ReadChar
.Stop:
pop di
pop si
pop bx
ret
; Search for next file matching DS:SI from Directory in ES:BX
; Return pointer to next matching file in ES:BX or
; BX = 0xFFFF (NOT_FOUND) if no match is found.
FindNextFile:
push si
push di
mov dx, si ; Preserve original filename in DX
.DirLoop:
cmp bx, [cs:RootMaxIdx]
jae .NotFound
mov al, [es:bx]
and al, al
jz .NextEntry
cmp al, ENTRY_DELETED
je .NextEntry
mov di, bx
mov si, dx
mov cl, 11
.Compare:
lodsb
cmp al, '?'
je .Wild
cmp al, [es:di]
jne .NextEntry
.Wild:
inc di
dec cl
jnz .Compare
; Match!
jmp short .Done
.NextEntry:
add bx, DIR_ENTRY_SIZE
jmp .DirLoop
.NotFound:
mov bx, NOT_FOUND
.Done:
pop di
pop si
ret
; Try to find CurFileName in Root Directory
; Returns pointer to directory entry in BX
; or 0xFFFF (NOT_FOUND)
; DS must point to CS
FindFileInRoot:
push si
mov ax, [RootSeg]
mov es, ax
xor bx, bx
mov si, CurFileName
call FindNextFile
pop si
ret
; Search for next file based on Find File structure in ES:BX
; Returns carry clear if file found
; carry set and error code in AX if not found
DoFindFile:
push si
push di
.Redo:
; CX = Where to start search
; FF_ROOT_IDX points to last checked entry
mov cx, [es:bx+FF_ROOT_IDX]
add cx, DIR_ENTRY_SIZE
push es
push bx
; Point DS:SI at search template
mov ax, es
mov ds, ax
mov si, bx
add si, FF_STEMPLATE
; Point ES:BX at root dir
mov ax, [cs:RootSeg]
mov es, ax
mov bx, cx
call FindNextFile
mov si, es
mov ds, si