-
Notifications
You must be signed in to change notification settings - Fork 1
/
EDLIN.ASM
681 lines (626 loc) · 9 KB
/
EDLIN.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
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
FCB EQU 5CH
RENAM EQU 23
OPEN EQU 15
CLOSE EQU 16
MAKE EQU 22
DELFIL EQU 19
RDBLK EQU 39
WRBLK EQU 40
SETDMA EQU 26
PRINTBUF EQU 9
OUTCH EQU 2
INBUF EQU 10
RR EQU 33
PROMPT EQU "*"
ORG 100H
PUT 100H
MOV SP,STACK
MOV DX,BREAK
MOV AX,2523H
INT 33
MOV SI,BAK
MOV DI,FCB+9
MOV CX,3
;File must not have .BAK extension
REPE
CMPB
JZ NOTBAK
;Open input file
MOV AH,OPEN
MOV DX,FCB
INT 33
MOV [HAVEOF],AL
OR AL,AL
JZ HAVFIL
MOV DX,NEWFIL
MOV AH,PRINTBUF
INT 33
HAVFIL:
MOV SI,FCB
MOV DI,FCB2
MOV CX,9
REP
MOVB
MOV SI,BAK
MOVW
MOVB
MOV AH,DELFIL
MOV DX,FCB2
INT 33
MOV AL,"$"
MOV DI,FCB2+9
STOB
STOB
STOB
MOV AH,DELFIL
INT 33
;Create .$$$ file to make sure directory has room
MOV AH,MAKE
INT 33
OR AL,AL
JZ SETUP
MOV DX,NODIR
JMP ERROR
NOTBAK:
MOV DX,NOBAK
JMP ERROR
TOOBIG:
MOV DX,TOBIG
JMP ERROR
NOTEOF:
MOV DX,NOEOF
JMP ERROR
SETUP:
MOV DX,START
MOV AH,SETDMA
INT 33
MOV CX,[6]
SUB CX,80H
MOV [LAST],CX
MOV DI,START-1
TEST B,[HAVEOF],-1
JNZ SAVEND
CALL NUMRECS
MOV [FCB+RR],0
MOV DX,FCB
MOV AH,RDBLK
INT 33
CMP AL,2
JZ TOOBIG
JCXZ SAVEND
ROR CX
XCHG CL,CH
ADD DI,CX ;Point to last byte
DOWN
MOV AL,1AH
REPNE
SCAB
JNZ NOTEOF
REPE
SCAB
INC DI
SAVEND:
CLD
INC DI
MOV B,[DI],1AH
MOV [ENDTXT],DI
MOV B,[COMBUF],128
MOV B,[EDITBUF],255
MOV [POINTER],START
MOV [CURRENT],1
COMMAND:
MOV SP,STACK
MOV AL,PROMPT
CALL OUT
MOV DX,COMBUF
MOV AH,INBUF
INT 33
MOV AL,10
CALL OUT
MOV [PARAM2],0
MOV B,[QFLG],0
MOV SI,2+COMBUF
CALL GETNUM
MOV [PARAM1],DX
CALL SKIP1
CMP AL,","
JNZ CHKNXT
INC SI
CHKNXT:
DEC SI
CALL GETNUM
MOV [PARAM2],DX
CALL SKIP1
CMP AL,"?"
JNZ DISPATCH
MOV [QFLG],AL
CALL SKIP
DISPATCH:
AND AL,5FH
MOV DI,COMTAB
MOV CX,NUMCOM
REPNE
SCAB
JNZ COMERR
MOV BX,CX
SHL BX
CALL [BX+TABLE]
JMP COMMAND
SKIP:
LODB
SKIP1:
CMP AL," "
JZ SKIP
RET
COMERR:
MOV DX,BADCOM
MOV AH,PRINTBUF
INT 33
JP COMMAND
GETNUM:
CALL SKIP
CMP AL,"."
JZ CURLIN
CMP AL,"#"
JZ MAXLIN
MOV DX,0
NUMLP:
CMP AL,"0"
JB RET
CMP AL,"9"
JA RET
SUB AL,"0"
MOV BX,DX
SHL DX
SHL DX
ADD DX,BX
SHL DX
CBW
ADD DX,AX
LODB
JP NUMLP
CURLIN:
MOV DX,[CURRENT]
LODB
RET
MAXLIN:
MOV DX,-1
LODB
RET
COMTAB DB "SFDLIE",13
NUMCOM EQU $-COMTAB
;-----------------------------------------------------------------------;
; Carefull changing the order of the next two tables. They are
; linked and chnges should be be to both.
TABLE DW NOCOM ;No command--edit line
DW ENDED
DW INSERT
DW LIST
DW DELETE
DW COMERR
DW COMERR
FINDLIN:
; Inputs
; BX = Line number to be located in buffer (0 means last line)
; Outputs:
; DX = Actual line found
; DI = Pointer to start of line DX
; Zero set if BX = DX
; AL,CX destroyed. No other registers affected.
MOV DX,[CURRENT]
MOV DI,[POINTER]
CMP BX,DX
JZ RET
JA FINDIT
MOV DX,1
MOV DI,START
CMP BX,DX
JZ RET
FINDIT:
MOV CX,[ENDTXT]
SUB CX,DI
MOV AL,10
OR AL,AL ;Clear zero flag
FINLIN:
JCXZ RET
REPNE
SCAB
INC DX
CMP BX,DX
JNZ FINLIN
RET
SHOWNUM:
; Inputs:
; BX = Line number to be displayed
; Function:
; Displays line number on terminal in 8-character
; format, suppressing leading zeros.
; AX, CX, DX destroyed. No other registers affected.
PUSH BX
MOV AL," "
CALL OUT
CALL CONV10
MOV AL,":"
CALL OUT
MOV AL," "
CALL OUT
POP BX
RET
CONV10:
;Inputs:
; BX = Binary number to be displayed
; Function:
; Ouputs binary number. Five digits with leading
; zero suppression. Zero prints 5 blanks.
XOR AX,AX
MOV DL,AL
MOV CX,16
CONV:
SHL BX
ADC AL,AL
DAA
XCHG AL,AH
ADC AL,AL
DAA
XCHG AL,AH
ADC DL,DL
LOOP CONV
MOV BL,"0"-" "
XCHG AX,DX
CALL LDIG
MOV AL,DH
CALL DIGITS
MOV AL,DL
DIGITS:
MOV DH,AL
SHR AL
SHR AL
SHR AL
SHR AL
CALL LDIG
MOV AL,DH
LDIG:
AND AL,0FH
JZ ZERDIG
MOV BL,0
ZERDIG:
ADD AL,"0"
SUB AL,BL
JMP OUT
LIST:
MOV BX,[PARAM1]
OR BX,BX
JNZ CHKP2
MOV BX,[CURRENT]
SUB BX,10
JA CHKP2
MOV BX,1
CHKP2:
CALL FINDLIN
JNZ RET
MOV SI,DI
MOV DI,[PARAM2]
INC DI
SUB DI,BX
JA DISPLAY
MOV DI,21
JP DISPLAY
DISPONE:
MOV DI,1
DISPLAY:
; Inputs:
; BX = Line number
; SI = Pointer to text buffer
; DI = No. of lines
; Function:
; Ouputs specified no. of line to terminal, each
; with leading line number.
; Outputs:
; BX = Last line output.
; All registers destroyed.
MOV CX,[ENDTXT]
SUB CX,SI
JZ RET
MOV BP,[CURRENT]
DISPLN:
PUSH CX
CALL SHOWNUM
POP CX
OUTLN:
LODB
CALL OUT
CMP AL,10
LOOPNZ OUTLN
JCXZ RET
DEC DI
JZ RET
CMP BX,BP
LAHF
INC BX
SAHF
JZ OUTLF
CMP BX,BP
JNZ DISPLN
OUTLF:
MOV AL,10
CALL OUT
JP DISPLN
RET: RET
NOCOM:
MOV BX,[PARAM1]
OR BX,BX
JNZ HAVLIN
MOV BX,[CURRENT]
INC BX ;Default is current line plus one
HAVLIN:
CALL FINDLIN
MOV SI,DI
MOV [CURRENT],DX
MOV [POINTER],SI
JNZ RET
CMP SI,[ENDTXT]
JZ RET
MOV DI,2+EDITBUF
MOV CX,254
MOV DL,-1
LOADLP:
LODB
STOB
INC DL
CMP AL,13
LOOPNZ LOADLP
MOV [EDITBUF+1],DL
MOV [OLDLEN],DL
MOV SI,[POINTER]
CALL DISPONE
CALL SHOWNUM
MOV AH,INBUF ;Get input buffer
MOV DX,EDITBUF
INT 33
MOV AL,10
CALL OUT
MOV CL,[EDITBUF+1]
MOV CH,0
JCXZ RET
MOV DL,[OLDLEN]
MOV DH,0
MOV SI,2+EDITBUF
MOV DI,[POINTER]
REPLACE:
; Inputs:
; CX = Length of new text
; DX = Length of original text
; SI = Pointer to new text
; DI = Pointer to old text in buffer
; Function:
; New text replaces old text in buffer and buffer
; size is adjusted. CX or DX may be zero.
; CX, SI, DI all destroyed. No other registers affected.
CMP CX,DX
JZ COPYIN
PUSH SI
PUSH DI
PUSH CX
MOV SI,DI
ADD SI,DX
ADD DI,CX
MOV AX,[ENDTXT]
SUB AX,DX
ADD AX,CX
CMP AX,[LAST]
JAE MEMERR
XCHG AX,[ENDTXT]
MOV CX,AX
SUB CX,SI
CMP SI,DI
JA DOMOV
ADD SI,CX
ADD DI,CX
STD
DOMOV:
INC CX
REP
MOVB
CLD
POP CX
POP DI
POP SI
COPYIN:
REP
MOVB
RET
DELETE:
MOV BX,[PARAM1]
OR BX,BX
JNZ DELFIN1
MOV BX,[CURRENT]
DELFIN1:
CALL FINDLIN
JNZ RET
MOV [CURRENT],BX
MOV [POINTER],DI
PUSH DI
MOV BX,[PARAM2]
OR BX,BX
JNZ DELFIN2
MOV BX,DX
DELFIN2:
INC BX
CALL FINDLIN
MOV DX,DI
POP DI
SUB DX,DI
XOR CX,CX
JP REPLACE
GETTEXT:
; Inputs:
; SI points into command line buffer
; DI points to result buffer
; Function:
; Moves [SI] to [DI] until ctrl-Z (1AH) or
; RETURN (0DH) is found. Termination char not moved.
; Outputs:
; AL = Termination character
; CX = No of characters moved.
; SI points one past termination character
; DI points to next free location
XOR CX,CX
GETIT:
LODB
CMP AL,1AH
JZ RET
CMP AL,0DH
JZ RET
STOB
INC CX
JP GETIT
MEMERR:
MOV DX,MEMFUL
MOV AH,PRINTBUF
INT 33
JMP COMMAND
INSERT:
MOV BX,[PARAM1]
OR BX,BX
JNZ INS
MOV BX,[CURRENT]
INC BX
INS:
CALL FINDLIN
MOV CX,[ENDTXT] ;Get End-of-text marker
MOV SI,CX
SUB CX,DI ;Calculate number of bytes to copy
INC CX
MOV DI,[LAST]
DOWN
REP
MOVB
XCHG SI,DI
UP
INC DI
MOV BP,SI
MOV BX,DX
INLP:
CALL SHOWNUM
MOV DX,EDITBUF
MOV AH,INBUF
INT 33
MOV AL,10
CALL OUT
MOV SI,EDITBUF+2
CMP B,[SI],1AH
JZ ENDINS
MOV CL,[SI-1]
MOV CH,0
MOV DX,SI
ADD DX,CX
INC DX
CMP DX,BP ;Will it fit?
JNC MEMERR
REP
MOVB
MOVB
MOV AL,10
STOB
INC BX
JP INLP
ENDINS:
MOV [POINTER],DI
MOV [CURRENT],BX
MOV SI,BP
INC SI
MOV CX,[LAST]
SUB CX,BP
REP
MOVB
DEC DI
MOV [ENDTXT],DI
RET
ENDED:
;Write text out to .$$$ file
MOV DI,[ENDTXT]
MOV CX,128
MOV AL,1AH
REP
STOB
MOV CX,[ENDTXT]
CALL NUMRECS
INC CX
MOV [FCB2+RR],0
MOV DX,FCB2
MOV AH,WRBLK
INT 33
OR AL,AL
JNZ WRTERR
;Close .$$$ file
MOV AH,CLOSE
INT 33
MOV SI,FCB
LEA DI,[SI+16]
MOV DX,SI
MOV CX,9
REP
MOVB
MOV SI,BAK
MOVW
MOVB
;Rename original file .BAK
MOV AH,RENAM
INT 33
MOV SI,FCB
MOV DI,FCB2+16
MOV CX,6
REP
MOVW
;Rename .$$$ file to original name
MOV DX,FCB2
INT 33
JMP 0
WRTERR:
MOV DX,DSKFUL
ERROR:
MOV AH,PRINTBUF
INT 33
JMP 0
NUMRECS:
SUB CX,START
AND CL,80H
ROL CX
XCHG CL,CH
RET
OUT:
PUSH DX
XCHG AX,DX
MOV AH,OUTCH
INT 33
XCHG AX,DX
POP DX
RET
BREAK:
IRET
BAK: DB "BAK"
NOBAK: DB "Cannot edit .BAK file--rename file$"
NODIR: DB "No room in directory for file$"
TOBIG: DB "File too big to fit into memory$"
NOEOF: DB "No end-of-file mark found in file$"
DSKFUL: DB "Disk full--file write not completed$"
MEMFUL: DB 13,10,"Memory full",13,10,"$"
BADCOM: DB "Entry error",13,10,"$"
NEWFIL: DB "New file",13,10,"$"
FCB2: DS 36
PARAM1: DS 2
PARAM2: DS 2
QFLG: DS 1
HAVEOF: DS 1
OLDLEN: DS 1
CURRENT: DS 2
POINTER: DS 2
LAST: DS 2
ENDTXT: DS 2
COMBUF: DS 2+128
EDITBUF: DS 2+255
DS 40
ALIGN
STACK:
START: