-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3 int21.asm
157 lines (123 loc) · 3.58 KB
/
3 int21.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
; this sample gets a string from a user, then it prints it out.
; this example uses dos interrupt: 21h
name "int21"
org 100h
jmp start
; when no file path is specified the default file path is:
; c:\emu8086\MyBuild
; if path is specified, it is emulated in:
; c:\emu8086\vdrive\
; for example, c:\test.txt is c:\emu8086\vdrive\c\test.txt
; this is done to prevent disasters and accidental overwriting of valuable files.
; relative paths such as "c:\mydir\..\autoexec.bat" are disabled for the same reasons.
; declarations:
; buffers hold ascii chars for INT 21h/0Ah input function,
; first byte is its maximum size, second byte: actual size.
; format of INT 21h/0Ah input buffer:
; -byte-offset- -number-of-bytes- -description-
; 00 1 buffer size
; 01 1 number of characters actually read (excluding cr).
; 02 1 to buffer-size actual buffer contents (including cr).
s1 db 100,?, 100 dup(' ')
t2 db 100,?, 100 dup(' ')
filename db 30,?, 30 dup(' ') ; file name should be in 8.3 - dos compatible format.
; file handle:
handle dw 0
; set segment registers to code:
start: mov ax, cs
mov ds, ax
mov es, ax
jmp m1
msg1 db "enter any text: $"
m1: mov dx, offset msg1
mov ah, 9
int 21h
; input a string:
mov dx, offset s1
mov ah, 0ah
int 21h
jmp m2
msg2 db 0Dh,0Ah,"enter file name: $"
m2: mov dx, offset msg2
mov ah, 9
int 21h
; input filename:
mov dx, offset filename
mov ah, 0ah
int 21h
; set 0 to the end of the filename:
xor bx, bx
mov bl, filename[1] ; get actual size.
mov filename[bx+2], 0
; create new file:
mov cx, 0
mov ah, 3ch
mov dx, offset filename+2
int 21h
jc error
mov handle, ax
; write buffer to file:
mov ah, 40h
mov bx, handle
mov dx, offset s1+2
xor cx, cx
mov cl, s1[1]
int 21h
jc error
; close file
mov bx, handle
mov ah, 3eh
int 21h
jc error
; open existing file:
mov al, 0 ; read.
mov ah, 3dh
mov dx, offset filename+2
int 21h
jc error
mov handle, ax
; read bytes from file
mov ah, 3fh
mov bx, handle
xor cx, cx
mov cl, t2[0]
mov dx, offset t2[2]
int 21h
jc error
mov t2[1], al
; close file
mov bx, handle
mov ah, 3eh
int 21h
jc error
jmp m3
msg3 db 0Dh,0Ah,"read from file: $"
m3: mov dx, offset msg3
mov ah, 9
int 21h
; set '$' to the end of the buffer:
xor bx, bx
mov bl, t2[1] ; get actual size.
mov t2[bx+2], '$'
; print the buffer:
mov dx, offset t2[2] ; skip 2 control bytes.
mov ah, 9
int 21h
jmp ok
; print error message:
error: jmp m5
msg5 db 0Dh,0Ah,"error...",0Dh,0Ah,'$'
m5: mov dx, offset msg5
mov ah, 9
int 21h
ok:
jmp m4
msg4 db 0Dh,0Ah,"press any key...",0Dh,0Ah,'$'
m4: mov dx, offset msg4
mov ah, 9
int 21h
mov ah, 0
int 16h
; exit to the operating system:
mov ah, 4ch
int 21h