
TITLE FCBWRIT.EXE -- Use FCB to write file of names
;--------------------------------------------------
stack segment para stack 'stack'
      dw  80 dup(?)
stack ends
;--------------------------------------------------
dseg segment para 'data'
reclen	 equ	32
namepar  label byte	    ;name parameter list
maxlen	 db reclen	    ;max length of name
namelen	 db ?		    ;act length of name
namedta  db reclen dup(' ')    ; DTA

fcbrec	 label byte	    ;FCB for disk file
fcbdriv	 db 0		    ;drive C
fcbname	 db 'NAMEFILE'	    ;file name
fcbext	 db 'DAT'	    ;extension
fcbblk	 dw 0		    ;current block #
fcbrcsz	 dw ?		    ;record size
fcbflsz	 dd ?		    ;DOS file size
	 dw ?		    ;DOS date
	 dt ?		    ;DOS reserved
fcbsqrc	 db 0		    ;current record #
	 dd ?		    ;relative record #

crlf	 db 13,10,'$'
errcde	 db 0
prompt	 db 'name ? ','$'
row	 db 01
opnmsg	 db '*** open error ***','$'
wrtmsg	 db '*** write error ***','$'
dseg ends
;--------------------------------------------------
cseg segment para 'code'
begin proc far
	assume cs:cseg, ds:dseg, ss:stack, es:dseg
	push	ds
	sub	ax,ax
	push	ax
	mov	ax,dseg
	mov	ds,ax
	mov	es,ax
	mov	ah,6		;scroll up func.
	mov	al,0
	call	scro		;clear screen
	call	curs		;set cursor
	call	createf		;create file,set DTA
	cmp	errcde,0	;available space?
	jz	contin		;yes,continue
	ret			;no, return to DOS
contin:	call	inputf
	cmp	namelen,0	;end of input?
	jne	contin		;no,continue
	call	clsef		;yes,close
	ret			;return to DOS
begin	endp

;	open disk file :
;--------------------------------------------------
createf proc near
	mov	ah,16h		;request create
	lea	dx,fcbrec
	int	21h
	cmp	al,0		;available space?
	jnz	err1		;no,error

	mov	fcbrcsz,reclen	;record size (equ)
	lea	dx,namedta	;set address of DTA
	mov	ah,1ah
	int	21h
	ret
err1:	lea	dx,opnmsg	;error message
	call	errm
	ret
createf endp

;	accept input :
;--------------------------------------------------
inputf proc near
	mov	ah,09		;request display
	lea	dx,prompt	;display prompt
	int	21h

	mov	ah,0ah		;request input
	lea	dx,namepar	;accept name
	int	21h
	call	disp		;handle scrolling
	cmp	namelen,0
	jne	blank		;yes, process
	ret			;no, exit
blank:	mov	bh,0
	mov	bl,namelen
	mov	namedta[bx],' '	;store blank
	call	writef		;call write routine

	cld
	lea	di,namedta	;clear
	mov	cx,reclen/2	;  name
	mov	ax,2020h	;   field
	rep	stosw
	ret			;exit
inputf endp

;	scroll,set cursor :
;--------------------------------------------------
disp proc near
	mov	ah,09		;request display
	lea	dx,crlf		;CR / LF
	int	21h		;call DOS
	cmp	row,18		;bottom of screen?
	jne	pass		;yes,bypass
	inc	row		;no,add to row
	ret
pass:	mov	ah,06		;scroll one row
	mov	al,01
	call	scro
	call	curs		;reset cursor
	ret
disp endp

;	write disk record :
;--------------------------------------------------
writef proc near
	mov	ah,15h		;request write
	lea	dx,fcbrec
	int	21h
	cmp	al,0		;valid write
	jz	valid		;yes
	lea	dx,wrtmsg	;no
	call	errm		;call error routine
	mov	namelen,0
valid:	ret
writef endp

;	close disk file :
;--------------------------------------------------
clsef proc near
	mov	namedta,1ah	;set EOF mark
	call	writef
	mov	ah,10h		;request close
	lea	dx,fcbrec
	int	21h
	ret
clsef endp

;	scroll screen :
;--------------------------------------------------
scro proc near			;AX set on entry
	mov	bh,1eh		;set yellow on blue
	mov	cx,0
	mov	dx,184fh
	int	10h		;scroll
	ret
scro endp

;	set cursor :
;--------------------------------------------------
curs proc near
	mov	ah,02
	mov	bh,0
	mov	dl,0
	mov	dh,row		;set cursor
	int	10h
	ret
curs endp

;	disk error routine :
;--------------------------------------------------
errm proc near
	mov	ah,09		;DX contains
	int	21h		; address of message
	mov	errcde,01	;set error code
	ret
errm endp
;--------------------------------------------------
cseg ends
     end begin
