
TITLE RANREAD.COM -- Random read records created
;		     by fcbwrit
cseg segment para 'code'
	assume cs:cseg, ds:cseg
	assume ss:cseg, es:cseg
	org    100h
begin:	jmp	main
;--------------------------------------------------
fcbrec	label byte	    ;FCB for disk file
fcbdriv	 db  1		    ;drive A
fcbname	 db  'namefile'	    ;file name
fcbext	 db  'dat'	    ;extension
fcbblk	 dw  0		    ;current block #
fcbrcsz	 dw  0		    ;record size
	 dd  ?		    ;DOS file size
	 dw  ?		    ;DOS date
	 dt  ?		    ;DOS reserved
	 db  0		    ;current record #
fcbrnrc	 dd  0		    ;random record #

reclen	equ  32		    ;record length
recdpar label byte	    ;parameter list:
maxlen	 db  3
actlen	 db  ?
recdno	 db  3 dup(' ')

namefld	 db  reclen dup(' '),13,10,'$'  ;DTA
openmsg	 db  '*** open error ***',13,10,'$'
readmsg	 db  '*** read error ***',13,10,'$'
col	 db  0
prompt	 db  'record number ? $'
row	 db  0
endcde	 db  0
;--------------------------------------------------
main proc near
	mov	ax,cseg		
	mov	ds,ax		

	call	clrscn		;clear screen
	call	curs		;set cursor
	call	openr		;open file,set DTA
	cmp	endcde,0	;valid open?
	jz	contin		;yes,continue
	mov	ax,4c00h	;no,terminate
	int	21h
contin:	call	recn		;request record #
	cmp	actlen,0	;any more requests?
	je	a2		;no,exit
	call	readr		;random read
	cmp	endcde,0	;normal read ?
	jnz	a1		;no, bypass
	call	dispr		;call display routine
a1:	jmp	contin
a2:	mov	ax,4c00h	;terminate
	int	21h
main endp
;	open disk routine :
;--------------------------------------------------
openr proc 
	mov	ah,0fh		;request open
	lea	dx,fcbrec
	int	21h
	cmp	al,0		;valid open ?
	jnz	b1		;no,error
	mov	fcbrcsz,reclen	;record  size(EQU)
	mov	ah,1ah
	lea	dx,namefld	;set address of DTA
	int	21h
	ret
b1:	lea	dx,openmsg
	call	errm
	ret
openr endp
;	get record number :
;--------------------------------------------------
recn proc near
	mov	ah,09h		;request display
	lea	dx,prompt
	int	21h
	mov	ah,0ah		;request input
	lea	dx,recdpar
	int	21h
	cmp	actlen,01	;check length 0,1,2
	jb	c3		;length 0,terminate
	ja	c1
	sub	ah,ah		;length
	mov	al,recdno
	jmp	c2
c1:	mov	ah,recdno	;length 2
	mov	al,recdno+1
c2:	and	ax,0f0fh	;clear ASCII 3's
	aad			;convert to binary
	mov	word ptr fcbrnrc,ax
c3:	mov	col,20
	call	curs		;set cursor
	ret
recn endp
;	read disk record :
;--------------------------------------------------
readr proc near
	mov	endcde,0	;clear indicator
	mov	ah,21h		;random read
	lea	dx,fcbrec
	int	21h
	cmp	al,0		;normal read ?
	jz	d1		;yes,exit
	lea	dx,readmsg	;no,invalid
	call	errm		;call error routine
d1:     ret
readr endp
;	display name :
;--------------------------------------------------
dispr proc near
	mov	ah,09		;request display
	lea	dx,namefld
	int	21h
	inc	row
	mov	col,0
	ret
dispr endp
;	clear screen :
;--------------------------------------------------
clrscn proc near
	mov	ax,0600h	;request scroll
	mov	bh,41h		;color(07 for BW)
	mov	cx,0
	mov	dx,184fh
	int	10h
	ret
clrscn endp
;	set cursor :
;--------------------------------------------------
curs proc near
	mov	ah,2		;request set
	mov	bh,0		;  cursor
	mov	dh,row
	mov	dl,col
	int	10h
	ret
curs endp
;	disk error routine :
;--------------------------------------------------
errm proc near
	mov	ah,09		;DX contains addr
	int	21h		;  of message
	inc	row
	mov	endcde,01
	ret
errm endp
;--------------------------------------------------
cseg ends
     end   begin
