-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-operations.asm
158 lines (120 loc) · 2.81 KB
/
file-operations.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
name "fileio"
; general rules for file system emulation:
; 1. the emulator emulates all drive paths in c:\emu8086\vdrive\
; for example: the real path for "c:\test1" is "c:\emu8086\vdrive\c\test1"
; 2. paths without drive letter are emulated to c:\emu8086\MyBuild\
; for example: the real path for "myfile.txt" is "c:\emu8086\MyBuild\myfile.txt"
; 3. if compiled file is running outside of the emulator rules 1 and 2 do not apply.
; ==================================================================================
; run this example slowly in step-by-step mode and observe what it does.
; ==================================================================================
org 100h
jmp start
dir1 db "c:\test1", 0
dir2 db "test2", 0
dir3 db "newname", 0
file1 db "c:\test1\file1.txt", 0
file2 db "c:\test1\newfile.txt", 0
file3 db "t1.txt", 0
handle dw ?
text db "lazy dog jumps over red fox."
text_size = $ - offset text
text2 db "hi!"
text2_size = $ - offset text2
start:
mov ax, cs
mov dx, ax
mov es, ax
; create c:\emu8086\vdrive\C\test1
mov dx, offset dir1
mov ah, 39h
int 21h
; create c:\emu8086\MyBuild\test2
mov dx, offset dir2
mov ah, 39h
int 21h
; rename directory: c:\emu8086\MyBuild\test2 to c:\emu8086\MyBuild\newname
mov ah, 56h
mov dx, offset dir2 ; existing.
mov di, offset dir3 ; new.
int 21h
; create and open file: c:\emu8086\vdrive\C\test1\file1.txt
mov ah, 3ch
mov cx, 0
mov dx, offset file1
int 21h
jc err
mov handle, ax
; write to file:
mov ah, 40h
mov bx, handle
mov dx, offset text
mov cx, text_size
int 21h
; close c:\emu8086\vdrive\C\test1\file1.txt
mov ah, 3eh
mov bx, handle
int 21h
err:
nop
; rename fileL c:\emu8086\vdrive\C\test1\file1.txt to c:\test1\newfile.txt
mov ah, 56h
mov dx, offset file1 ; existing.
mov di, offset file2 ; new.
int 21h
; delete file c:\emu8086\vdrive\C\test1\newfile.txt
mov ah, 41h
mov dx, offset file2
int 21h
; delete directory: c:\emu8086\vdrive\C\test1
mov ah, 3ah
mov dx, offset dir1
int 21h
; create and open file: c:\emu8086\MyBuild\t1.txt
mov ah, 3ch
mov cx, 0
mov dx, offset file3
int 21h
jc err2
mov handle, ax
; seek:
mov ah, 42h
mov bx, handle
mov al, 0
mov cx, 0
mov dx, 10
int 21h
; write to file:
mov ah, 40h
mov bx, handle
mov dx, offset text
mov cx, text_size
int 21h
; seek:
mov ah, 42h
mov bx, handle
mov al, 0
mov cx, 0
mov dx, 2
int 21h
; write to file:
mov ah, 40h
mov bx, handle
mov dx, offset text2
mov cx, text2_size
int 21h
; close c:\emu8086\MyBuild\t1.txt
mov ah, 3eh
mov bx, handle
int 21h
err2:
nop
; delete file c:\emu8086\MyBuild\t1.txt
mov ah, 41h
mov dx, offset file3
int 21h
; delete directory: c:\emu8086\MyBuild\newname
mov ah, 3ah
mov dx, offset dir3
int 21h
ret