-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab5.asm
564 lines (478 loc) · 10.8 KB
/
lab5.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
.model small
.stack 100h
.data
res_handle dw 0
handle dw 0
filename db 50 DUP(0)
filepath db 50 DUP(0)
filename_size db 0
is_end db 0
symbol db 0
extension_flag db 1
line_begining_dx dw 0
line_begining_ax dw 0
string db 201;allowed
db ?;data entered
db 201 dup('$');can be entered
same_string db 201 dup('$')
should_write db 0
error_sizemsg db "Wrong or no command promt arguments", 10, 13, '$'
enter_msg db "enter string: ", 10, 13, '$'
find_err db "Can't find file!", 10, 13, 0, '$'
path_err db "Can't find file path!", 10, 13, 0, '$'
toomany_err db "To many opened file!", 10, 13, 0, '$'
accessdenied_err db "Access denied!", 10, 13, 0, '$'
incorrectacces_err db "Incorrect access mode!", 10, 13, 0, '$'
string_err_msg db "Size of string is 0, reenter string!", 10, 13, 0, '$'
.code
;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
changeOffset MACRO min, newOffset
local cnt_change, cnt_changeoffset
mov ah, 42h
mov al, 1
mov bx, handle
mov dx, newOffset
mov cl, 0
cmp cl, min
je cnt_change
neg dx
mov cx, 0FFFFh
jmp cnt_changeoffset
cnt_change:
mov cx, 0
cnt_changeoffset:
int 21h
ENDM
;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
print MACRO str
push ax
push dx
mov dx, offset str
mov ah, 09h
int 21h
pop dx
pop ax
ENDM
;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
check_extension PROC
mov al, [di]
cmp al, '.'
jne wrong_extension
inc di
mov al, [di]
cmp al, 't'
jne wrong_extension
inc di
mov al, [di]
cmp al, 'x'
jne wrong_extension
inc di
mov al, [di]
cmp al, 't'
jne wrong_extension
ret
wrong_extension:
mov extension_flag, 0
ret
check_extension ENDP
;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
newline PROC
mov ah, 0Eh
mov al,0Dh
int 10h
mov ah, 0Eh
mov al,0Ah
int 10h
ret
newline ENDP
;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
openFile PROC
jmp openFile_start
cant_find_error:
print find_err
mov is_end, 1
jmp openFile_fin
path_error:
print path_err
mov is_end, 1
jmp openFile_fin
toomany_error:
print toomany_err
mov is_end, 1
jmp openFile_fin
access_error:
print accessdenied_err
mov is_end, 1
jmp openFile_fin
accessmode_error:
print incorrectacces_err
mov is_end, 1
jmp openFile_fin
openFile_start:
mov dx, offset filepath
mov al, 2h ;openfile(rw)
mov ah, 3Dh
int 21h
jc openFile_fin_err ;if error, terminate
mov bx, ax ;handler to bx
mov handle, bx
jmp openFile_fin
openFile_fin_err:
cmp ax, 02h
je cant_find_error
cmp ax, 03h
je path_error
cmp ax, 04h
je toomany_error
cmp ax, 05h
je access_error
cmp ax, 0Ch
je accessmode_error
openFile_fin:
ret
openFile ENDP
;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
createFile PROC
jmp createFile_start
cant_find_error_1:
print find_err
mov is_end, 1
jmp createFile_fin
path_error_1:
print path_err
mov is_end, 1
jmp createFile_fin
toomany_error_1:
print toomany_err
mov is_end, 1
jmp createFile_fin
access_error_1:
print accessdenied_err
mov is_end, 1
jmp createFile_fin
accessmode_error_1:
print incorrectacces_err
mov is_end, 1
jmp createFile_fin
createFile_start:
mov dx, offset filename ;address of file to dx
mov ah, 3Ch
mov al, 00h
mov cx, 0000h
int 21h ;call the interupt
jc createFile_fin_err ;if error occurs, terminate program
mov bx, ax ;put handler to file in bx
mov res_handle, bx
jmp createFile_fin
createFile_fin_err:
cmp ax, 02h
je cant_find_error_1
cmp ax, 03h
je path_error_1
cmp ax, 04h
je toomany_error_1
cmp ax, 05h
je access_error_1
cmp ax, 0Ch
je accessmode_error_1
createFile_fin:
ret
createFile ENDP
;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
writeLine PROC
jmp writeLine_start
writeLine_start:
;seting pos to chosen coord
mov ah, 42h
mov al, 0
mov bx, handle
mov cx, line_begining_dx
mov dx, line_begining_ax
int 21h
writeLineFor:
mov bx,handle
mov cx,1 ;read one symbol
mov ah,3Fh ;read from the opened file (its handler in bx)
mov dx,offset symbol;where to read
int 21h
cmp ax,0
je writeLineEnd
mov al,10
cmp al,symbol
je writeLineEnd
mov al,13
cmp al,symbol
je writeLineEnd
mov bx,res_handle
mov cx,1
mov ah,40h ;write ro result.txt
mov dx,offset symbol
int 21h
jmp writeLineFor
set_end_file_0_fin:
mov is_end,1
jmp writeLine_fin
writeLineEnd:
mov symbol,13;/r
mov bx,res_handle
mov cx,1
mov ah,40h
mov dx,offset symbol
int 21h
mov symbol,10;/n
mov bx,res_handle
mov cx,1
mov ah,40h
mov dx,offset symbol
int 21h
call skip_eol
writeLine_fin:
ret
writeLine ENDP
;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
nulling_string PROC
mov di, offset same_string
mov al, symbol
for_8:
mov ah, [di]
cmp ah, '$'
je fin_null
cmp ah, al
jne cnt_null
mov ah, 0
mov [di], ah
cnt_null:
inc di
jmp for_8
fin_null:
ret
nulling_string ENDP
;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
cmp_nulling PROC
mov should_write, 0
mov di, offset same_string
jmp for_7
should_write_set_1:
mov should_write, 1
jmp fin_cmp
for_7:
mov al, [di]
cmp al, '$'
je should_write_set_1
cmp al, 0
jne fin_cmp
inc di
jmp for_7
fin_cmp:
ret
cmp_nulling ENDP
;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
skip_eol PROC
jmp skip_start
set_end_1_:
mov is_end,1
jmp fin_skip
skip_start:
mov bx,handle
mov cx,1
mov ah,3fh
mov dx,offset symbol
int 21h
cmp ax,0
je set_end_1_
mov al,13
cmp al,symbol
je skip_start
mov al,10
cmp al,symbol
je skip_start
changeOffset 1,1
fin_skip:
ret
skip_eol ENDP
;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
checkLine PROC
jmp check_start
set_should_0_fin:
mov should_write, 0
jmp cnt
set_end_1_fin:
mov is_end, 1
jmp cnt
check_start:
call copy_string;copying string to same_string
mov is_end, 0
checkLineFor:
mov bx, handle
mov cx, 1
mov ah, 3fh
mov dx, offset symbol
int 21h
cmp ax, 0;eof
je set_end_1_fin
mov al, 10; /n
cmp al, symbol
je set_should_0_fin
call nulling_string
call cmp_nulling
cmp should_write, 1
je cnt
jmp checkLineFor
cnt_skip:
mov bx, handle
mov cx, 1
mov ah, 3fh
mov dx, offset symbol
int 21h
cmp ax, 0;eof
je set_end_1_fin
mov al, 10; /r
cmp al, symbol
jne cnt_skip
cnt:
ret
checkLine ENDP
;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
writeToNew PROC
push ax
push bx
push cx
push dx
checkingLines:
;save pos of line
changeOffset 0, 1
mov line_begining_dx, dx
dec ax
mov line_begining_ax, ax
changeOffset 1, 1
call checkLine
cmp is_end, 1
je writetofile
cmp should_write, 0
je checkingLines
call writeLine
jmp checkingLines
writetofile:
;call writeLine
writeToNewEnd:
pop dx
pop cx
pop bx
pop ax
ret
writeToNew ENDP
;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
copy_string PROC
;copying entered string to same string to verify line
mov di, offset string + 2
mov si, offset same_string
for_9:
mov al, [di]
cmp al, '$'
je for_9_end
mov [si], al
inc si
inc di
jmp for_9
for_9_end:
inc si
mov [si], al
ret
copy_string ENDP
;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
start:
mov ax, @data
mov ds, ax
xor cx, cx
mov cl, es:[80h]
cmp cl, 0 ;ci size 0, no parametrs
je terminate_bcsize
cmp cl, 12
jl terminate_bcsize
mov si, 81h ;skip space
xor di,di
inc si ;skip space
dec cl ;skip space
get_parm1:
mov al, es:si
cmp al, ' '
je end_get_param1
cmp al, 0Dh
je terminate_bcsize
mov [filepath + di], al ;setting to filepath
inc di
dec cx
inc si
jmp get_parm1
end_get_param1:
inc si
xor di, di
dec cx
cmp cx, 1
je terminate_bcsize
get_parm2:
mov al, es:si
mov [filename + di] , al
inc di
inc si
inc filename_size
loop get_parm2 ;while cx not equal 0
mov di, offset filename
xor ax, ax
mov al, filename_size
add di, ax
sub di, 4
call check_extension
cmp extension_flag, 0
je terminate_bcsize
jmp string_input
terminate_bcsize:
mov ah, 9
mov dx, offset error_sizemsg
int 21h
jmp terminate
string_error:
call newline
mov ah, 9
mov dx, offset string_err_msg
int 21h
call newline
jmp string_input
string_input:
mov ah, 9
mov dx, offset enter_msg
int 21h
mov ah, 0Ah
mov dx, offset string
int 21h
mov si, offset string + 1
mov cl, [si]
mov ch, 0
cmp cx, 0
je string_error
inc cx
add si, cx
mov al, '$'
mov [si], al
open_file_test:
call openFile
cmp is_end, 1
je terminate
call createFile
cmp is_end, 1
je terminate
;jmp close_file
call writeToNew
close_file:
mov ah, 3Eh
mov bx, handle
int 21h
mov ah, 3Eh
mov bx, res_handle
int 21h
terminate:
mov ax, 4C00h
int 21h
int 20h
ends
end start