-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexe_reloc.asm
More file actions
2637 lines (2262 loc) · 49.1 KB
/
Copy pathexe_reloc.asm
File metadata and controls
2637 lines (2262 loc) · 49.1 KB
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
; -----------------------------------------------------------
; Minimal MZ (.EXE) relocator — called from Stage 2
; Expects: DS = LOAD_SEG (0x1010), where file is loaded
; ES = PSP_SEG (0x1000)
; Returns: carries out relocation, sets up PSP, far-jumps to CS:IP
; 1000% 8086 compatible (no 386+ instructions, no FS/GS, correct SP)
; -----------------------------------------------------------
do_exe_reloc:
; ---- Parse MZ header from DS:0 (0x1010:0) ----
mov ax, [ds:0x0E] ; AX = initial_SS
mov dx, [ds:0x10] ; DX = initial_SP
mov bp, [ds:0x14] ; BP = initial_IP
mov di, [ds:0x16] ; DI = initial_CS
; Save program stack/entry values on relocator's stack
push ax ; [sp+6] = initial_SS
push dx ; [sp+4] = initial_SP
push bp ; [sp+2] = initial_IP
push di ; [sp] = initial_CS
; ---- 1. Apply relocations directly to source image BEFORE copying ----
; This avoids corruption when the copy loop overlaps and overwrites the source header/reloc table.
mov cx, [ds:0x06] ; CX = count of relocations
mov si, [ds:0x18] ; SI = reloc table offset in DS (0x1010)
mov ax, es
add ax, 0x10 ; AX = target program segment (0x1010)
mov bx, [ds:0x08] ; BX = header size in paragraphs
mov bp, ds
add bp, bx ; BP = source program image segment (0x1010 + header_paragraphs)
.reloc_loop:
jcxz .reloc_done
mov bx, [ds:si] ; BX = offset of word to patch
mov dx, [ds:si+2] ; DX = relative segment of word to patch
add si, 4
add dx, bp ; DX = absolute patch segment in source
push es
mov es, dx
add [es:bx], ax ; adjust the segment pointer by target_segment (0x1010)
pop es
loop .reloc_loop
.reloc_done:
; ---- 2. Calculate image size in paragraphs from MZ header ----
mov ax, [ds:0x04] ; AX = pages
mov bx, [ds:0x02] ; BX = bytes in last page
test bx, bx
jz .last_page_zero
dec ax ; AX = pages - 1
shl ax, 1
shl ax, 1
shl ax, 1
shl ax, 1
shl ax, 1 ; AX = AX * 32
add bx, 15
shr bx, 1
shr bx, 1
shr bx, 1
shr bx, 1 ; BX = BX / 16 (paragraphs)
add ax, bx ; AX = total file paragraphs
jmp .sub_header
.last_page_zero:
shl ax, 1
shl ax, 1
shl ax, 1
shl ax, 1
shl ax, 1 ; AX = AX * 32
.sub_header:
mov bx, [ds:0x08] ; BX = header size in paragraphs
sub ax, bx ; AX = image size in paragraphs
mov bp, ax ; BP = image size in paragraphs
; ---- Set free_mem_ptr dynamically based on program size + min_alloc ----
mov ax, LOAD_SEG
add ax, bp ; AX = program end segment
mov dx, [ds:0x0A] ; DX = min_alloc paragraphs from MZ header
add ax, dx ; AX = program end segment + min_alloc
mov [cs:free_mem_ptr], ax
; ---- 3. Copy image to 0x1010:0000 ----
push ds
push es
; DS = 0x1010 + header paragraphs
mov ax, ds
add ax, [ds:0x08]
mov ds, ax
; ES = 0x1000 + 0x10
mov ax, es
add ax, 0x10
mov es, ax
.copy_loop:
test bp, bp
jz .copy_done
mov cx, 512 ; 512 paragraphs = 8192 bytes = 4096 words
cmp bp, cx
jae .do_copy
mov cx, bp ; Copy remaining paragraphs
.do_copy:
sub bp, cx ; BP = paragraphs left after this copy
; Save paragraph count to update segment registers later
push cx
; Convert paragraphs in CX to words for rep movsw: words = paragraphs * 8
shl cx, 1
shl cx, 1
shl cx, 1 ; CX = CX * 8
xor si, si
xor di, di
rep movsw
pop cx ; Restore paragraph count
test bp, bp
jz .copy_done
; Increment segments by CX paragraphs
mov ax, ds
add ax, cx
mov ds, ax
mov ax, es
add ax, cx
mov es, ax
jmp .copy_loop
.copy_done:
pop es ; ES = 0x1000
pop ds ; DS = 0x1010
; ---- 4. Zero PSP area (256 bytes) at ES:0 (0x1000:0) ----
push es
pop di ; DI = 0x1000
push di
xor di, di ; ES:DI = 0x1000:0
xor ax, ax
mov cx, 128 ; 128 words
rep stosw
pop di ; DI = 0x1000
; ---- 5. Build PSP fields at ES:0 ----
mov byte [es:0], 0xCD ; INT 20h
mov byte [es:1], 0x20
mov word [es:2], 0x9FFF ; Link to top of conventional memory (640KB limit)
mov word [es:0x0A], 0
mov word [es:0x0C], 0
mov word [es:0x0E], 0
mov word [es:0x2C], 0 ; Environment segment (0 = none)
; ---- Install minimal INT 21h handler ----
push es
xor ax, ax
mov es, ax ; ES = 0x0000
mov word [es:0x0084], int21_handler
mov word [es:0x0086], cs ; CS of stage2
; ---- Save and Install minimal INT 16h handler ----
mov ax, [es:0x0058]
mov [cs:old_int16_ip], ax
mov ax, [es:0x005a]
mov [cs:old_int16_cs], ax
mov word [es:0x0058], int16_handler
mov word [es:0x005a], cs
; ---- Install minimal INT 20h handler ----
mov word [es:0x0080], int20_handler
mov word [es:0x0082], cs
pop es ; Restore ES (0x1000)
; ---- 6. Set up registers & jump ----
cli
pop di ; DI = initial_CS
pop bp ; BP = initial_IP
pop dx ; DX = initial_SP
pop bx ; BX = initial_SS
mov ax, es ; AX = 0x1000
add ax, 0x10 ; AX = 0x1010
add di, ax ; DI = absolute CS
add bx, ax ; BX = absolute SS
mov ss, bx
mov sp, dx ; SP = initial_SP
mov ax, es
mov ds, ax
mov es, ax
sti
; DBG: about to jump to program ('J')
push ax
mov al, 0x4A
out 0xE9, al
pop ax
push di ; CS (absolute)
push bp ; IP (entry offset)
xor ax, ax
xor bx, bx
xor cx, cx
xor dx, dx
xor si, si
xor di, di
xor bp, bp
retf ; jump!
int20_handler:
cld
jmp int21_handler.exit
; =============================================================
; Minimal INT 16h (Keyboard API) handler with Debounce
; =============================================================
int16_handler:
cmp ah, 0x00
je .handle_read
cmp ah, 0x10
je .handle_read
cmp ah, 0x01
je .handle_check
cmp ah, 0x11
je .handle_check
jmp far [cs:old_int16_ip]
; ---- Blocking read (AH=00h/10h) ----
; Caller only reads AX on return, not flags. Don't touch FLAGS_caller.
.handle_read:
push ax ; [1] Save function code (AH=00h or 10h)
push di ; [2] Save DI (will hold tick)
push ds ; [3] Save DS (will be set to 0)
pushf
call far [cs:old_int16_ip] ; BIOS returns key in AX
; Read BIOS tick into DI without polluting other regs
push bx
xor bx, bx
mov ds, bx ; DS = 0
mov di, [0x046C] ; DI = BIOS tick
pop bx ; BX unchanged
pop ds ; [3] DS restored
cmp ah, [cs:last_key_scan]
jne .read_pass
push di
sub di, [cs:last_key_tick]
cmp di, 1
pop di
jb .read_drop
.read_pass:
mov [cs:last_key_scan], ah
mov [cs:last_key_tick], di
pop di ; [2] DI restored
add sp, 2 ; [1] Discard saved AX; keep BIOS AX in register
iret ; Flags = caller's original flags (IF untouched)
.read_drop:
pop di ; [2] DI restored
pop ax ; [1] Restore function code so BIOS call uses 00h/10h
jmp .handle_read ; Duplicate consumed; wait for next key
; ---- Status check (AH=01h/11h) ----
; Caller checks ZF. Return BIOS ZF but PRESERVE caller's IF/DF/TF (bits 8-10).
.handle_check:
push ax ; [1] Save function code
pushf
call far [cs:old_int16_ip] ; BIOS: ZF=0 if key, ZF=1 if none
pushf ; [2] Save BIOS flags (has ZF from keyboard status)
jz .check_no_key ; ZF=1 = no key
; Key available — check tick for debounce
push bx ; [3] Save BX
push di ; [4] Save DI
push ds ; [5] Save DS
xor bx, bx
mov ds, bx
mov di, [0x046C] ; DI = tick
pop ds ; [5] Restore DS
; DI=tick, DS=restored, stack: [DI_orig, BX_orig, BIOS_flags, AX_orig, IP, CS, FLAGS]
cmp ah, [cs:last_key_scan]
jne .check_pass
push di
sub di, [cs:last_key_tick]
cmp di, 1
pop di
jb .check_drop
.check_pass:
pop di ; [4] DI restored
pop bx ; [3] BX restored
; Stack: [BIOS_flags, AX_orig, IP, CS, FLAGS_caller]
; Merge: BIOS_flags but keep caller's IF/DF/TF (mask 0x0700)
push bp
mov bp, sp
push cx
push bx
; [bp+2]=BIOS_flags [bp+10]=FLAGS_caller (BP offsets stable despite push cx/bx)
mov cx, [bp+2]
and cx, 0xF8FF ; Clear IF/DF/TF from BIOS flags
mov bx, [bp+10]
and bx, 0x0700 ; Keep IF/DF/TF from caller
or cx, bx
mov [bp+10], cx ; Write combined flags (BIOS ZF + caller IF)
pop bx
pop cx
pop bp
popf ; Consume BIOS_flags from stack
add sp, 2 ; Discard AX_orig; AX = BIOS key
iret
.check_no_key:
; Stack: [BIOS_flags, AX_orig, IP, CS, FLAGS_caller]
push bp
mov bp, sp
push cx
push bx
mov cx, [bp+2]
and cx, 0xF8FF
mov bx, [bp+10]
and bx, 0x0700
or cx, bx
mov [bp+10], cx
pop bx
pop cx
pop bp
popf
add sp, 2 ; Discard AX_orig; keep BIOS AX
iret
.check_drop:
pop di ; [4] DI restored
pop bx ; [3] BX restored
popf ; [2] Consume BIOS_flags
pop ax ; [1] Restore function code (01h or 11h)
; Consume duplicate key via blocking read
push ax
push bx
push cx
push dx
push si
push di
push ds
push es
dec ah ; 01h→00h, 11h→10h
pushf
call far [cs:old_int16_ip]
pop es
pop ds
pop di
pop si
pop dx
pop cx
pop bx
pop ax ; AX = AX_orig (01h or 11h)
jmp .handle_check
; =============================================================
; Minimal INT 21h (DOS API) handler
; =============================================================
int21_handler:
pushf
push ax
mov al, '['
out 0xE9, al
mov al, ah
call dbg_print_hex
mov al, ']'
out 0xE9, al
pop ax
cmp ah, 0x00 ; Terminate program
je .exit
cmp ah, 0x4C ; Exit
je .exit
cmp ah, 0x0E ; Select Default Drive
je .select_default_drive
cmp ah, 0x19 ; Get Default Drive
je .get_default_drive
cmp ah, 0x1A ; Set DTA
je .set_dta
cmp ah, 0x47 ; Get Current Directory
je .get_current_directory
cmp ah, 0x30 ; Get DOS version
je .get_ver
cmp ah, 0x09 ; Print string
je .print_str
cmp ah, 0x0B ; Check input status
je .chk_input
cmp ah, 0x08 ; Read char without echo
je .read_char
cmp ah, 0x07 ; Read char without echo
je .read_char
cmp ah, 0x06 ; Direct console I/O
je .direct_io
cmp ah, 0x02 ; Character output
je .char_out
cmp ah, 0x2C ; Get time
je .get_time
cmp ah, 0x62 ; Get PSP
je .get_psp
cmp ah, 0x51 ; Get PSP (same handler)
je .get_psp
cmp ah, 0x4A ; Resize block
je .resize_mem
cmp ah, 0x48 ; Allocate memory
je .alloc_mem
cmp ah, 0x49 ; Free memory
je .free_mem
cmp ah, 0x35 ; Get vector
je .get_vector
cmp ah, 0x25 ; Set vector
je .set_vector
cmp ah, 0x01 ; Read char with echo
je .read_char_echo
cmp ah, 0x0C ; Clear buffer and input
je .clear_buffer_and_input
cmp ah, 0x3C ; Create File
je create_file_api
cmp ah, 0x3D ; Open File
je open_file_api
cmp ah, 0x3E ; Close File
je close_file_api
cmp ah, 0x3F ; Read File
je read_file_api
cmp ah, 0x40 ; Write File
je write_file_api
cmp ah, 0x41 ; Delete File
je delete_file_api
cmp ah, 0x42 ; Lseek
je lseek_api
cmp ah, 0x43 ; Get/Set Attributes
je get_set_attributes
; Unhandled functions return Carry Set
popf
push bp
mov bp, sp
or word [bp+6], 0x0001 ; Set Carry Flag (bit 0) in caller's flags
pop bp
cmp ah, 0x4E ; Find First
je .err_file_not_found
mov ax, 1 ; Error code 1 (invalid function)
iret
.err_file_not_found:
mov ax, 2 ; Error code 2 (file not found)
iret
.err_invalid_handle:
mov ax, 6 ; Error code 6 (invalid handle)
iret
.write_file:
popf
cmp bx, 1 ; stdout
je .write_stdout
cmp bx, 2 ; stderr
je .write_stdout
; Other handles are invalid
push bp
mov bp, sp
or word [bp+6], 0x0001 ; Set Carry Flag (bit 0) in caller's flags
pop bp
mov ax, 6 ; Invalid handle
iret
.write_stdout:
push ds
push si
push cx
push bx
push ax
mov si, dx ; DS:SI = buffer
mov bx, cx ; BX = save count
.write_loop:
jcxz .write_done
lodsb
push cx
mov ah, 0x0E
xor bh, bh
int 10h
pop cx
dec cx
jmp .write_loop
.write_done:
pop ax
pop bx
pop cx
pop si
pop ds
; Return AX = BX (actual count written)
mov ax, bx
jmp .clear_cf_and_iret
.exit:
cld
sti
push cs
pop ds
; Restore INT 16h vector to original BIOS handler to prevent crash on reboot
push es
xor ax, ax
mov es, ax
mov ax, [cs:old_int16_ip]
mov [es:0x0058], ax
mov ax, [cs:old_int16_cs]
mov [es:0x005a], ax
pop es
mov si, exit_msg
.exit_print:
lodsb
test al, al
jz .exit_wait
out 0xE9, al
mov ah, 0x0E
xor bh, bh
int 10h
jmp .exit_print
.exit_wait:
xor ah, ah
int 16h
; ---- Soft reboot: manually reload the boot sector ----
; js-dos (DOSBox) has no real BIOS: after `boot beautiful.img`,
; INT 19h / JMP FFFF:0000 hang the emulator instead of rebooting.
; INT 13h is fully emulated for the mounted floppy, so we re-read
; LBA 0 to 0000:7C00 ourselves and jump to it (warm boot).
cli
xor ax, ax
mov ss, ax
mov sp, 0x7C00 ; fresh stack just below the boot sector
sti
xor ax, ax
mov dl, [cs:cur_drive]
int 13h ; reset disk controller
mov cx, 3 ; retry up to 3 times
.exit_read_boot:
push cx
xor ax, ax
mov es, ax
mov bx, 0x7C00 ; ES:BX = 0000:7C00
mov ax, 0x0201 ; AH=02h read, AL=1 sector
mov cx, 0x0001 ; cylinder 0, sector 1
xor dh, dh ; head 0
mov dl, [cs:cur_drive]
int 13h
pop cx
jnc .exit_boot_ok
loop .exit_read_boot
; Fallback: BIOS warm reboot (real hardware / v86 with real BIOS)
cli
int 19h
jmp 0xFFFF:0000
.exit_boot_ok:
mov dl, [cs:cur_drive] ; DL = boot drive, as the BIOS would pass it
jmp 0x0000:0x7C00
.get_ver:
popf
mov ax, 0x0005 ; DOS 5.0
xor bx, bx
xor cx, cx
jmp .clear_cf_and_iret
.print_str:
push ds
push si
push ax
mov si, dx
.print_loop:
lodsb
cmp al, '$'
je .print_done
mov ah, 0x0E
xor bh, bh
int 10h
jmp .print_loop
.print_done:
pop ax
pop si
pop ds
popf
jmp .clear_cf_and_iret
.chk_input:
popf
push ax
mov ah, 0x01
int 16h
jz .chk_no_key
pop ax
mov al, 0xFF
iret
.chk_no_key:
pop ax
xor al, al
iret
.read_char:
popf
push bp
mov bp, sp
push bx
cmp byte [cs:ext_key_scan], 0
je .read_no_cached
mov al, [cs:ext_key_scan]
mov byte [cs:ext_key_scan], 0
jmp .read_char_done
.read_no_cached:
xor ah, ah
int 16h ; AL = character, AH = scan code
test al, al
jnz .read_char_done
mov [cs:ext_key_scan], ah
xor al, al
.read_char_done:
pop bx
pop bp
jmp .clear_cf_and_iret
.read_char_echo:
popf
push bp
mov bp, sp
push bx
cmp byte [cs:ext_key_scan], 0
je .read_echo_no_cached
mov al, [cs:ext_key_scan]
mov byte [cs:ext_key_scan], 0
jmp .read_char_echo_done
.read_echo_no_cached:
xor ah, ah
int 16h ; AL = character, AH = scan code
test al, al
jnz .read_echo_normal
mov [cs:ext_key_scan], ah
xor al, al
jmp .read_char_echo_done
.read_echo_normal:
push ax
mov ah, 0x0E ; TTY echo
xor bh, bh
int 10h
pop ax
.read_char_echo_done:
pop bx
pop bp
jmp .clear_cf_and_iret
.clear_buffer_and_input:
popf
mov ah, al ; execute sub-function in AL
jmp int21_handler
.direct_io:
cmp dl, 0xFF
je .direct_in
push ax
push bx
mov al, dl
mov ah, 0x0E
xor bh, bh
int 10h
pop bx
pop ax
popf
jmp .clear_cf_and_iret
.direct_in:
popf
push bp
mov bp, sp
push ax
cmp byte [cs:ext_key_scan], 0
je .direct_no_cached
mov al, [cs:ext_key_scan]
mov byte [cs:ext_key_scan], 0
pop bx
mov ah, bh
and word [bp+6], ~0x0041
pop bp
iret
.direct_no_cached:
mov ah, 0x01
int 16h
jz .direct_no_key
xor ah, ah
int 16h
test al, al
jnz .direct_normal_key
mov [cs:ext_key_scan], ah
xor al, al
jmp .direct_return_key
.direct_normal_key:
.direct_return_key:
pop bx
mov ah, bh
and word [bp+6], ~0x0041
pop bp
iret
.direct_no_key:
pop bx
xor al, al
mov ah, bh
or word [bp+6], 0x0040
and word [bp+6], ~0x0001
pop bp
iret
.char_out:
push ax
push bx
mov al, dl
mov ah, 0x0E
xor bh, bh
int 10h
pop bx
pop ax
popf
jmp .clear_cf_and_iret
.get_time:
popf
push ax
push bx
push si
push di
push bp
push ds
push es
xor ax, ax
int 1Ah ; CX:DX = ticks (from BIOS)
mov ax, dx ; AX = ticks
xor dx, dx
mov bx, 18
div bx ; AX = total seconds, DX = ticks remainder (0-17)
; centiseconds = (remainder * 11) / 2
push ax
mov ax, dx
mov bx, 11
mul bx
shr ax, 1
mov bp, ax ; BP = centiseconds (0-93)
pop ax
; seconds & total minutes
xor dx, dx
mov bx, 60
div bx ; AX = total minutes, DX = seconds (0-59)
push dx
; minutes & hours
xor dx, dx
mov bx, 60
div bx ; AX = total hours, DX = minutes (0-59)
push dx
; hours (0-23)
xor dx, dx
mov bx, 24
div bx ; DX = hours (0-23)
mov ch, dl ; CH = hours
pop ax ; AX = minutes
mov cl, al ; CL = minutes
pop ax ; AX = seconds
mov dh, al ; DH = seconds
mov ax, bp ; AX = centiseconds
mov dl, al ; DL = centiseconds
pop es
pop ds
pop bp
pop di
pop si
pop bx
pop ax
jmp .clear_cf_and_iret
.select_default_drive:
popf
mov [cs:cur_drive], dl
mov al, 2 ; Return total number of logical drives (2)
jmp .clear_cf_and_iret
.get_default_drive:
popf
mov al, [cs:cur_drive]
jmp .clear_cf_and_iret
.set_dta:
popf
jmp .clear_cf_and_iret
.get_current_directory:
popf
push ds
push es
push si
push di
push cx
push ax
mov ax, [cs:current_dir_cluster]
test ax, ax
jz .cwd_root
mov cx, ax ; CX = target cluster
mov word [cs:current_dir_cluster], 0
call read_root_dir ; ES = ROOT_DIR_SEG
jc .cwd_err
mov ax, ROOT_DIR_SEG
mov ds, ax ; DS = ROOT_DIR_SEG
xor si, si ; DS:SI points to root dir entries
mov dx, 224 ; 224 root dir entries
.cwd_scan_loop:
cmp byte [ds:si], 0
je .cwd_err
cmp byte [ds:si], 0xE5
je .cwd_next_entry
mov al, [ds:si+11] ; attribute
test al, 0x08 ; volume label?
jnz .cwd_next_entry
test al, 0x10 ; subdirectory?
jz .cwd_next_entry
mov ax, [ds:si+26] ; starting cluster
cmp ax, cx
je .cwd_found
.cwd_next_entry:
add si, 32
dec dx
jnz .cwd_scan_loop
jmp .cwd_err
.cwd_found:
; DS:SI points to the directory entry in ROOT_DIR_SEG.
; Destination is caller's DS:SI.
; Set ES = caller's DS (at sp+10), DI = caller's SI (at sp+6).
mov bp, sp
mov ax, [bp+10] ; AX = caller's DS
mov es, ax
mov di, [bp+6] ; DI = caller's SI
; Copy name (up to 8 characters, removing trailing spaces)
mov cx, 8
.cwd_copy:
lodsb
cmp al, ' '
je .cwd_copy_end
stosb
loop .cwd_copy
.cwd_copy_end:
; Null terminate
xor al, al
stosb
pop ax
pop cx
pop di
pop si
pop es
pop ds
jmp .clear_cf_and_iret
.cwd_root:
; Root directory is just empty string
mov bp, sp
mov ax, [bp+10]
mov es, ax
mov di, [bp+6]
xor al, al
stosb
pop ax
pop cx
pop di
pop si
pop es
pop ds
jmp .clear_cf_and_iret
.cwd_err:
pop ax
pop cx
pop di
pop si
pop es
pop ds
mov ax, 15 ; Invalid drive
jmp .set_cf_and_iret
.get_psp:
popf
mov bx, PSP_SEG
jmp .clear_cf_and_iret
.resize_mem:
popf
; ES = block segment to resize, BX = new size in paragraphs
mov ax, es
add ax, bx ; AX = new free memory segment pointer
mov [cs:free_mem_ptr], ax
jmp .clear_cf_and_iret
.alloc_mem:
popf
push cx
mov ax, [cs:free_mem_ptr]
mov cx, 0x9FFF
sub cx, ax ; CX = available paragraphs
cmp bx, cx
ja .alloc_failed
; Success: allocate BX paragraphs
add bx, ax ; BX = new free_mem_ptr
mov [cs:free_mem_ptr], bx
pop cx
jmp .clear_cf_and_iret
.alloc_failed:
mov bx, cx ; BX = max available paragraphs
pop cx
push bp
mov bp, sp
or word [bp+6], 0x0001 ; Set Carry Flag (bit 0) in caller's flags
pop bp
mov ax, 8 ; Error 8: Insufficient memory
iret
.free_mem:
popf
jmp .clear_cf_and_iret
.get_vector:
push ds
push si
xor si, si
mov ds, si
mov si, ax
and si, 0x00FF