-
Notifications
You must be signed in to change notification settings - Fork 0
/
cw.asm
268 lines (238 loc) · 5.16 KB
/
cw.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
;Program READFILE.ASM: Read and display a text file.
;
;use16
FORMAT MZ
ORG 100H
JMP START
;Data segment
FILENAME DB "HELLO.TXT",0 ;Filename
HANDLE RW 1 ;Handele
FBUFF RB 1024 ;file data buffer
OEMSG DB 'Cannot open HELLO.TX.$' ;Open error
RFMSG DB 'Cannot read HELLO.TX.$' ;Read error
CFMSG DB 'Cannot close HELLO.TX.$' ;Close error
NAME RB 18 ;Name buffer
NAMECNTSTR RB 8 ;Name counted buffer
SPASECNT RB 1 ;Space count
NAMECNT RB 1 ;Name count
ENDLINE DB 13,10,'$' ;Endl
;END OF DATA SEGMENT
;Printout macro:
; Print result string with name contains in NAME and count of it in NAMECNT
;In args:
; NONE.
MACRO PRINTOUT
{
PUSH AX
PUSH BX
PUSH CX
PUSH DX
MOV DH,[NAMECNT]
MOV SI,NAMECNTSTR
MOV BL,10
DEC SI
LOOPDIV: ; Create from [NAMECNT] string like 01$
INC SI
MOVZX AX,DH
DIV BL
MOV DH,AL
ADD AH,'0'
MOV [SI],AH
OR DH,DH
JNE LOOPDIV
INC SI
MOV BYTE[SI],'$'
DEC SI
@@: ; Swap, cos aftre prew loop ten look like 01$, this loop make it normal 10$
MOV AL,[SI-1]
MOV AH,[SI]
CMP AL,'0'
JNGE @F
XCHG AL,AH
MOV [SI-1],AL
MOV [SI],AH
DEC SI
JMP @B
@@: ; Print out
MOVZX AX,[NAME+1]
MOV SI,NAME+2
ADD SI,AX
MOV BYTE[SI],' '
INC SI
MOV BYTE[SI],'$'
MOV AH,09H
MOV DX,NAME+2
INT 21H
MOV DX,NAMECNTSTR
INT 21H
MOV DX,ENDLINE
INT 21H
} ; END OF 'PRINTOUT' MACROS
;Print macro:
; Print data from FBUFF with CNT len
;In args:
; CNT - len of FBUFF
MACRO PRINT CNT
{
PUSH AX
PUSH BX
PUSH DX
MOV BX,FBUFF
ADD BX,CNT
MOV BYTE[BX],'$'
MOV AH,9
MOV DX,FBUFF
INT 21H
POP DX
POP BX
POP AX
} ; END OF 'PRINT' MACROS
;Parce macro:
; Count NAME in FBUFF with CNT len
;In args:
; CNT - len of FBUFF
MACRO PARCE LEN
{
PUSH AX
PUSH BX
PUSH CX
PUSH DX
MOV DL,[SPASECNT] ;Count of spaces
MOV DH,[NAMECNT] ;Count of name
MOV CX,LEN
MOV SI,FBUFF
.LOOPP: ;Main loop
MOV AL,[SI]
CMP AL,' '
JNE @F
INC DL
@@:
CMP DL,1
JNE .LOOPEND
INC SI
PUSH CX
PUSH BX
MOV DI,NAME+2
MOVZX CX,[NAME+1]
.COMPARE: ;String compare loop
MOV BL,[SI]
MOV BH,[DI]
CMP BL,BH
JNE .TOSPACE
INC SI
INC DI
LOOP .COMPARE
INC DH
.TOSPACE: ;Next space finder loop
MOV BL,[SI]
CMP BL,' '
JE @F
INC SI
JMP .TOSPACE
@@:
INC DL
POP BX
POP CX
.LOOPEND:
CMP DL,2
JNE @F
XOR DL,DL
@@:
INC SI
LOOP .LOOPP
MOV [SPASECNT],DL
MOV [NAMECNT],DH
POP DX
POP CX
POP BX
POP AX
}
;Open file macro:
; Just open file with name FILENAME
;In args:
; NONE.
OPENFILE:
MOV AH,3DH
XOR AL,AL
MOV DX,FILENAME
XOR CX,CX
INT 21H
JC .ERR
MOV [HANDLE],AX
RET
.ERR:
MOV DX,OEMSG
MOV AH,9
INT 21H
STC
RET
; END OF 'OPENFILE' MACROS
;Read file macro:
; Read data from [HANDLE], that contains in AX,
; and put data to FBUFF, than for test print it
;In args:
; NONE.
READFILE:
MOV BX,AX
.LOOP:
MOV AH,3FH
MOV DX,FBUFF
MOV CX,1024
INT 21H
JC .ERR
CMP AX,0
JZ .END
MOV DX,FBUFF
CMP DX,1AH
JZ .END
PARCE AX
jmp .LOOP
.ERR:
MOV DX,RFMSG
MOV AH,9
INT 21H
STC
RET
.END:
RET
; END OF 'READFILE' MACROS
;Close file macro:
; Close file using HANDLE
;In args:
; NONE.
CLOSEFILE:
MOV AH,3EH
MOV BX,[HANDLE]
INT 21H
JC .ERR
RET
.ERR:
MOV DX,CFMSG
MOV AH,9
INT 21H
STC
; END OF 'PRINT' MACROS
START:
MOV [SPASECNT],0
MOV [NAMECNT],0
;MOV [NAME],18
;MOV [NAME+1],3
;MOV [NAME+2],'L'
;MOV [NAME+3],'O'
;MOV [NAME+4],'L'
;MOV SI,NAME
;MOV CX,FBUFF
MOV AH,0AH
MOV [NAME],18
MOV DX,NAME
INT 21H
CALL OPENFILE ;open BUFF.ASM
JC EXIT ;jump if error
CALL READFILE ;read BUFF.ASM
CALL CLOSEFILE ;close BUFF.ASM
PRINTOUT
EXIT:
MOV AH,8
INT 21H
MOV AX,4C00H
INT 21H