Section 4
Programming Examples

	The programs presented in this section illustrate how to use the 
BDOS functions
described in the previous section. The examples show how to copy 
a file, how to
dump a file, how to create or access a random access file, and 
how to write an RSX
program.


4.1	A Sample File_To-File Copy Program

	The following program illustrates simple file operations. You can 
create the pro-
gram source file, COPY.ASM, using ED or another editor, and then 
assemble
COPY.ASM using MACTM. MAC produces the file COPY.HEX. Use the 
utility
HEXCOM to produce a COPY.COM file that can execute under CP/M 3.

	The COPY program first sets the stack pointer to a local area, 
then moves the
second name from the default area at OO6CH to a 33-byte file 
control block named
DFCB. The DFCB is then prepared for file operations by clearing 
the current record
field. Because the CCP sets up the source FCB at OOSCH upon entry 
to the COPY
program, the source and destination FCBs are now ready for 
processing. To prepare
the source FCB, the CCP places the first name into the default 
FCB, with the proper
fields zeroed, including the current record field at OO7CH.

	COPY continues by opening the source file, deleting any existing 
destination file,
and then creating the destination file. 1f each of these 
operations is successful, the
COPY program loops at the label COPY until each record is read 
from the source
file and placed into the destination file. Upon completion of the 
data transfer, the
destination file is closed, and the program returns to the CCP 
command level by
jumping to BOOT.
4.1	A Sample Copy Program	CP/M 3 Programmer's Guide
	;	sample file-to-file copy program
	;	at the ccp level , the command
	;	copy a:x.y b:u.v
	;	copies the file named x.y from drive
	;	a to a file named u.v on drive bf
	0000	=	boot	equ	0000h	;	system reboot
	0005	=	bdos	equ	0005h	; bdos entry Point
	005c	=	fcbl	equ	OOSch	;	first file name
	005c	=	sfcb	equ	fcbl	;	source fcb
OO6c =	fcbZ	equ	OOBch	; second file name
	0080	=	dbuff	equ	0080h	; default buffer
	0100 =	tPa	equ	0100h	; beginning of tpa
	0003	=	printf	equ	9	; Print buffer func*
	OOOf	=	openf	equ	15	;	open file func*
	0010	=	closef	equ	16	;	close file func*
	0013	=	deletef equ	19	; delete file func*
	0014	=	readf	equ	20	; sequential  read
	0015	=	writef	equ	21	; sequential write
	0016 =	makef	equ	22	; make file func*
	0100	org	tPa	; beginning of tpa
	0100 311b02	lxi	sp,stack; local stack
			;	move second file name to dfcb
	0103	OelO		mvi	c,16	; half an fcb
	0105	116c00		lxi	d,fcb2	;	source of move
	0108	2ldaOl		lxi	h,dfcb	;	destination fcb
	OlOb	la	mfcb:	ldax	d	;	source fcb
	OlOc	13		inx	d	;	ready next
	0l0d	77		mov	m,a	; dest fcb
	OlOe	23		inx	h	;	ready next
	OlOf	Od		dcr	c	; count l6...O
	0110	c2ObOl		jnz	mfcb	;	loop 16 times
	;	name has been moved, zero cr
	0113	af		xra	a	;	a = 00h
	0114	32fa01		sta	dfcbcr	;	current  rec = 0
CP/M 3 Programmer's Guide	4.1  A Sample Copy Program
	'	source and destination fcbs  ready
	0117	1l5c00	lxi	d,sfcb	;	source file
	0lla	cd69Ol	call	open	;	error if 255
	Olid	118701	lxi	d	,nofile;	ready message
	0120	3c	inr	a	;	255 becomes 0
	0121	cc6lOl	cz	finis	;	done if no file
	'	source file open, prep destination
	Ol24 lldaOl	lxi	d ,dfcb	;	destination
	0127 cd7301	call	delete	;	remove if Present
	OlZa	lldaOl	lxi	d,dfcb	;	destination
	OlZd	cdB2Ol	call	make	;	create the file
	0130	119601	lxi	d,nodir	;	ready message
	0133	3c	inr	a	;	255 becomes 0
	0134	cc6lOl	cz	finis	;	done if no dir space
	'	source file open, dest file open
	'	copy until end of file on source
	0137	115c00	copy:	lxi	d,sfcb	;	source
	013a	cd78Ol		call	read	;	read next  record
	013d	b7		ora	a	;	end of file?
	013e	c25101		Jnz	eofile	;	skip write if so
			;	not end	of file, write the record
	0141	lldaOl		lxi	d,dfcb	;	destination
	0144	cd7dOl		call	write	;	write  record
	0147	11a901		lxi	d,space	;	ready message
	014a	b7		ora	a	;	00 if write ok
	014b	c46101		cnz	finis	;	end if so
	014e	c33701		Jmp	copy	;	loop until eof

eofile:  ; end of file, close destination
	0151	lldaOl	lxi	d,dfcb	;	destination
	0154	cd6eOl	call	close	;	255 if error
	0157	2lbbOl	lxi	h,wrprot;		ready message
	015a	3c	inr	a	;	255 becomes 00
	015b	cc6lOl	cz	finis	;	should not happen
	;	copy operation complete, end
OlSe llccOl	lxi	d,normal ;  ready message
			finis:	; write	message	given by de,  reboot
	0161	0e09		mvi	c,printf
	0163	cdOSOO		call	bdos	; write message
	0166	c30000		Jmp	boot	;  reboot system
4.1	A Sample Copy Program	CP/M 3 Programmer's Guide
	'	system interface subroutines
	'	(all  return directly from bdos)
	0169 OeOf	open:	mUi	C ,openf
	01Gb c30500		Jmp	bdos
	016e OelO	close:	mvi	c,closef
	0170 c30500		Jmp	bdos
	0173 0e13	delete:	mvi	c ,deletef
	0175 c30500		Jmp	bdos
	0178 0e14	read:	mvi	C ,readf
	017a c3O5OO		Jmp	bdos
	017d 0e15	write:	mvi	c ,writef
	017f c3O5OO		Jmp	bdos
	0182 0e16	maKe:	mvi	c,makef
	0184 c30500		Jmp	bdos
		         '	console	messages
	0187	6e6f20fnofile:	db	'no source file$'
	0196	6e6f2O9nodir:	db	'no directory space$'
	01a9	Sf7s74fspace:	db	'out of data space$'
	Olbb	7772695wrprot:	db	'write protected?$'
	Olcc	636f700normal:	db	'copy complete$'
	'	data	areas
	Olda	dfcb:	ds	33	;	destination fob
	Olfa =	dfcbcr	equ	dfcb+32	;	current  record
	Olfb	ds	32	; 16 level stack
	stack:
	021b		end


	Note that this program makes several simplifications and could be 
enhanced. First,
it does not check for invalid filenames that could, for example, 
contain ambiguous
references. This situation could be detected by scanning the 
32-byte default area
starting at location OOSCH for ASCII question marks. To check 
that the filenames
have, in fact, been included, COPY could check locations OOSDH 
and OO6DH for
nonblank ASCII characters. Finally, a check should be made to 
ensure that the source
and destination filenames are different. Speed could be improved 
by buffering more
data on each read operation. For example, you could determine the 
size of memory
by fetching FBASE from location 0006H, and use the entire 
remaining portion of
memory for a data buffer. You could also use CP/M 3's 
Multi-Sector I/O facility to
read and write data in up to 16K units.
CP/M 3 Programmer's Guide	4.2  A Sample File Dump Utility


4*2 A Sample File Dump Utility

	The following dump program reads an input file specified in the 
CCP command
line, and then displays the content of each record in hexadecimal 
format at the
console.

; DUMP program reads input file and displays hex data
	0100			org	l00h
	0005	=	bdos	equ	0005h	;dos entry point
	0001	=	cons	equ	1	;read console
	0002	=	typef	equ	2	;type function
	0009	=	printf	equ	9	;buffer print entry
	000b	=	brkf	equ	11	;break key function  (true if char
	OOOf	=	openf	equ	15	;file open
	0014	=	readf	equ	20	;read function
	005c =	fcb	equ	Sch	;file control block address
	0080 =	buff	equ	80h	;input disk buffer address
	;	non graphic characters
	OOOd =	cr	equ	Odh	;carriage  return
	OOOa =	1f	equ	Oah	;line feed
			;	file	control block definitions
	005c	=	fcbdn	equ	fcb+O	;disk name
	005d	=	fcbfn	equ	fcb+1	;file name
	0065	=	fcbft	equ	fcb+B	;disk file tyPe	(3 characters)
	0068	=	fobrl	equ	fcb+12	;file's current	reel number
	00Gb	=	fcbrc	equ	fcb+15	;file's  record count  (0 to 128)
	007c	=	fcbcr	equ	fcb+32	;current  (next)	record number (0
	007d	=	fcbln	equ	fcb+33	;fcb length
		;	set up stack
	0100	210000	lxi	h,0
	0103	39	dad	sp
		;	entry stack Pointer in hi from the ccp
	0104	221502	shld	oldsp
		;	set sp to local stack area (restored at finis)
	0107	315702	lxi	sp,stktop
		;	read and Print successive buffers
	0l0a	cdclOl	call	setuP    ;set up input file
	0l0d	feff	cpi	255       ;255 if file not Present
	OlOf	c2lbOl	jnz	openok   ;skip if open is ok
4.2	A Sample File Dump Utility	CP/M 3 Programmer's Guide
			;	file not there, give error message and  return
	0112	11f301		lxi	d,opnmsg
	0115	cd9cOl		call	err
	0118	c35101		jmp	finis    ;to  return
			openok:	;open operation 0k, set buffer index to end
	Olib	3e80		mvi       a,80h
	Olid	321302		sta      ibp       ;set buffer pointer to 80h
			;	hl contains next address to print
	0120	210000		lxi       h,0       ;start with 0000
			gloop:
	0123	e5		push	  h	;save line position
	0124	cdaZOl		call	  gnb
	0127	el		pop	  h	;recall line position
	0128	da5lOl		jc	  finis	;carry set by gnb if end file
	012b	47		mov	  b,a
			;	print	hex values
			;	check	for line fold
	012c	7d		mov	  a,l
	012d	eBOf		ani	  Ofh	;check low 4 bits
	012f	c24401		jnz	  nonum
			;	print	line number
	0132	cd7201		call	  crlf
			;	check for break key
	0135 cdS9Ol	call	break
			;	accum lsb = 1 if character ready
	0138 0f	rrc	;into carry
0139	da5lOl	jc	finis	;do not print any more
; 013c 7c		mov	a,h
013d	cdBfOl	call	phex
0140	7d	mov	a,l
0141	cd8fOl	call	phex
	        non um:
	0144	23	inx	h	;to next line number
	0145	3e20	mvi	a,'  '
	0147	cd6SOl	call	pchar
	014a	78	mov	a,b
	014b	cd8fOl	call	phex
	014e	c32301	jmp	gloop
CP/M 3 Programmer's Guide	4.2  A Sample File Dump Utility
			finis:
			;	end of dump
	0151	cd7201		call	crlf
	0154	2a1502		lhld	oldsp
	0157	f9	sphl
	;	stack pointer contains cop's stack location
0158 c9                 ret                 ;to the ccp
			;	subroutines
			break:	;check break key  (actually any key will do)
	0159	e5d5c5		push h!	push d!  push b; environment saved
	015c	OeOb		mvi	c,brkf
	Ol5e	cdOSOO		call	bdos
	0161	cidlel		pop b!  pop d!  pop h; environment  restored
	0164	c9		ret
			pohar:	;Print a character
	0165	e5d5c5		push h!  push d!  push b; saved
	0188	0e02		mvi      c,typef
	016a	5f		mov      e ,a
	016b	cdOSOO		call     bdos
	016e	cidlel		pop b!  pop d!  pop h;  restored
	0171	c9		ret
			crlf
	0172	3eOd	mvi	a,cr
	0174	cd6501	call	pchar
	0177	3eOa	mvi	a,lf
	0179	cdBSOl	call	pchar
	017c	c9	ret
			pnib:	;print	nibble	in  reg a
	017d	e6Of		ani	 Ofh	  ;low 4 bits
	017f	feOa		cpi	 10
	0181	d28901		jnc	 p10
			;	less	than or equal to 9
	0184	c630		adi	 '0'
	0188	c38b01		jmp	 prn
			;	greater	or equal to 10
	0189	c637	pl0:	adi	'a' - 10
	018b	cd6501	prn	call	pchar
	018e	c9		ret
4.2	A Sample File Dump Utility	CP/M 3 Programmer's Guide
			phex:	;Print hex char in  reg a
	018f	f5		push	PSW
	0190	0f		rrc
	0191	0f		rrc
	0192	0f		rrc
	0193	0f		rrc
	0194	cd7dOl		call	pnib     ;print nibble
	0197	fi		pop	pSW
	0198	cd7dOl		call	pnib
	018b	c9		ret
	err:	;Print error message
			;	d,e addresSes message ending With"$"
	019c	0e09		mvi	c,printf          ;print buffer function
	019e	cdOSOO		call	bdos
	Olal	c9		ret
	gnb	;get next byte
01a2 3a1302		lda	ibp
Ola5 fe8O		cPi	80h
01a7 c2b301		jnz	gO
	;	read another buffer
Olaa	cdce0l		call	diskr
Olad	b7		ora	a	   ;zero value if  read 0k
Olae	cab3Ol		jz	go	   ;for another byte
		;	end of data,		return With carry Set for cof
Olbi	37		stc
01b2	c9		ret
			g0:	;read the byte at buff+reg a
	01b3	5f		mov	e,a       ;ls byte of buffer index
	01b4	1600		mvi	d,0       ;double precision index to de
	01b6	3c		inr	a         ;index=index+l
	01b7	321302		sta	ibp       ;back to memory
			;	Pointer	iS incremented
			;	save the current file address
	Olba	218000		lxi	h,buff
	Olbd	19		dad	d
			;	absolute character address iS in hl
	Olbe	7e		mov	a,m
			;	byte iS	in the accumulator
	Olbf	b7		ora	a         ;reset carry bit
	OlcO	c9		ret
CP/M 3 Programmer's Guide	4.2  A Sample File Dump Utility
	setup:	;set up file
	;	open the file for input
01c1 af		xra      a	;zero to accum
01c2 327c00		sta      fcbcr	;clear current record
OlcS 115c00	lxi	d,fcb
01c8 OeOf	mvi	c,openf
Olca cdOSOO	call	bdos
	;	255 in accum if open error
Olcd c9	ret
	diskr:	;read disk file  record
	Olce	eSdScS	Push h!	Push d!  Push b
	OldI	115c00	lxi	d,fcb
	01d4	0e14	mvi	c,readf
	01d6	cdOSOO	call	bdos
	01d9	cidlel	Pop b!  Pop d!  Pop h
	Oldc	c9	ret
	;	fixed message area
Oldd 46494c0signon: db	'file dumP version 2.0$'
01f3 Od0a4e0opnmsg: db	cr,lf ,'no input file Present on disk$'
	;	variable area
	0213	ibp:	ds	2	;input buffer Pointer
	0215	oldsp:	ds	2	;entry sP value from ccP
	;	stack area
	0217	ds	64	;reserve 32 level stack
		stktop:
	0257		end
4,3  A Sample Random Access Program	CP/M 3 Programmer's Guide


4.3	A Sample Random Access Program

	This example is an extensive but complete example of random 
access operation.
The following program reads or writes random records upon command 
from the
terminal. When the program has been created, assembled, and 
placed into a file
labeled RANDOM.COM, the CCP level command

A>RANDOM X.DAT


can start the test program. In this case, the RANDOM program 
looks for a file
X.DAT and, if it finds it, prompts the console for input. 1f 
X.DAT is not found,
RANDOM creates the file before displaying the prompt. Each prompt 
takes the
form:

next command?

and is followed by operator input, terminated by a carriage 
return, The input com-
mands take the form:

nW nR nF Q

where n is an integer value in the range 0 to 262143, and W, R, 
F, and Q are simple
command characters corresponding to random write, W, random read, 
R, random
write with zero fill, F, and quit processing, Q. 1f you enter a W 
or F command, the
RANDOM program issues the prompt:

tYPe data:

You then respond by typing up to 127 characters, followed by a 
carriage return.
RANDOM then writes the character string into the X.DAT file at 
record n. 1f you
enter an F command, the RANDOM program fills previously 
unallocated data blocks
with zeros before writing record n. 1f you enter the R command, 
RANDOM reads
record number n and displays the string value at the console. 1f 
you enter the Q
command, the X.DAT file is closed, and the program returns to the 
console com-
mand processor. In the interest of brevity, the only error 
message 15:

error, try again
CP/M 3 Programmer's Guide	4.3  A Sample Random Access Program


	The program begins with an initialization section where the input 
file is opened or
created, followed by a continuous loop at the label ready where 
the individual com-
mands are interpreted. The program uses the default file control 
block at O0SCH and
the default buffer at 0080H in all disk operations. The utility 
subroutines that follow
contain the principal input line processor, called readc, This 
particular program shows
the elements of random access processing and can be used as the 
basis for further
program development.
	;******************************
	;*	*
	;* sample  random access Program for cP/m 3	+
	;+	+
	;******************************
	0100	org	l00h	;base of tPa
	0000 =	reboot	equ	0000h	;system reboot
	0005 =	bdos	equ	0005h	;bdos entry Point
	0001	-	coninp	equ	1	;console input function
	0002	-	conout	equ	2	;console outPut function
	0009	-	pstring equ	9	;Print string until  '$'
	000a	=	rstring equ	10	;read console buffer
	000c	-	version equ	12	;return version number
	OOOF =	openf	equ	15	;file open function
	0010	-	closef	equ	16	;close function
	0016	-	makef	equ	22	;make file function
	0021	-	readr	equ	33	;read  random
	0022	-	Writer	equ	34	;write  random
	0028	-	Wrtrzf	equ	40	;Write  random zero fill
	0098 -	parsef	equ	152	;parse function
	005c	-	fcb	equ	OOSch	;default file control block
	007D	-	ranrec	equ	fcb+33	;random record Position
	007F	=	ranovf	equ	fcb+35	;high order (overflow)  byte
	0080	=	buff	equ	0080h	;buffer address
	OOOD =	or	equ	Odh	;carriage  return
	oooa =	1f	equ	Oah	;line feed
4.3	A Sample Random Access Program	CP/M 3 Programmer's Guide
	; +****++++***+++***+++++*+++*+***+++***************+
	;*	*
	;+ load 9P, Set-up file for random access	+
	;*	+
	;************************************
	0100 313703	lxi	sp,stack
			;	version	3.1?
	0103	OEOc		mvi	c,version
	0105	cDO5OO		call	bdos
	0108	FE2O		cPi	31h       ;version 3.1 or better?
	oioa	D21601		jnc	versok
			;	bad version, message and go back
	0l0D	118102		lxi	d,badver
	0110	cD3102		call	Print
	0113	c30000		jmp	reboot
	                        ve rsok :
			;	correct	version	for random access
	0116	QEOF		mvi	c,openf	;open default fob
	0118	3a5DOO	rdname:	lda	fcb+1
	011b	FE2O		cPi	'  '
	011d	c22c01		jnz	opfile
	0120	11E002		lxi	d,entmsg
	0123	cD3102		call	Print
	0126	cD2002		call	Parse
	0129	c31801		jmP	rdname
	012c	iiscoo	opfile:	lxi	d,fcb
	012F	cdOSOO		call	bdos
	0132	3c		inr	a	;err 255 becomes zero
	0133	c24501		jnz	ready
			;	cannot open file, so create it
	0136	0E16		mvi	c,makef
	0138	115c00		lxi	d,fcb
	013b	cdOSOO		call	bdos
	013E	3c		inr	a         ;err 255 becomes zero
	013F	c24b01		jnz	ready
			;	cannot create file, directory full
	0142	iiaooz		lxi	d,nospace
	0145	cd3102		call	print
	0148	c30000		jmp	reboot   ;back to ccP
CP/M 3 Programmer's Guide	4.3  A Sample Random Access Program


; **4***********************************************
	;*		*
	;*	loop back to "ready" after each command	*
	;*		*
; **************************************************
	ready:
	;	file is  ready for procesSing
	014b	cD3C02		call	readcom	;read next command
	014E	227D00		shld	ranrec	;store input  record*
	0151	217F00		lxi	h,ranovf
	0154	71		mov	m,c	;set ranrec high byte
	0155	FE51		cpi	'Q'	;quit?
	0157	C26901		jnz	notq
			;	quit processing,		close file
	oisa	OE1O		mvi	c,closef
	O1SC	115C00		lxi	d,fcb
	015F	CDO5OO		call	bdos
	0162	3C		inr	a	;err 255 becomes 0
	0163	CaFFOl		jz	error	;error meSsage ' retry
	0166	c30000		jmp	reboot	iback to ccP
	;******************************
	;*	*
	;* end of quit command, process write	*
	;*	*
	;***************************************************
	no tq:
	;	not the	quit command,  random Write?
0169 FES7		cpi	'W'
016b C29C01		jnz	notw
		            ;	this is a random Write, fill buffer until cr
	016E	11b302	lxi	  d,datmsg
	0171	CD3102	call	  Print	;data PromPt
	0174	OE7F	mvi	  c,127	;UP to 127 characters
	0176	218000	lxi	  h,buff	;destination
		            rloop:	;read	next character to buff
	0179	C5	push	  b	;save counter
	017a	E5	push	  h	inext destination
	017b	CD0802	call	  getchr	;character to a
	017E	El	pop	  h	+;irestore counter
	017F	C1	pop	  b	;restore next to fill
	0180	FEOD	cpi	  Cr	;end of line?
	0182	Ca8bOl	jz	  erloop
4.3	A Sample Random Access Program	CP/M 3 Programmer's Guide
			;	not end; Store character
	0185	77		mov	m,a
	0186	23		inx	 h	+;inext to fill
	0187	OD		dcr	 c	+;icoUnter goes down
	0188	C27901		jnz	 rloop	+;iend of buffer?
			e rloop:
			;	end of	read loop, Store 00
	018b	3600		mvi	 m,0
			;	write the  record		to selected  record number
	018D	0E22		mvi	c,Writer
	018F	115C00		lxi	d,fcb
	0192	CDOSOO		call	bdos
	0195	b7		ora	a	+;ierror code zero?
	0196	C2FFOI		jnz	error	+;imessage if not
	0199	C34bO1		jmp	ready	+;ifor another record


; ********************************************************
	;*	*
	;* end of Write command, process Write  random zero fill	*
	;*	*
+;i ********************************************************
	no tw:
	;	not the	quit command;  random Write zero fill?
019C FE46		cpi	'F'
019E C2CFOI		jnz	notf

;	this  is a random Write; fill buffer until cr
	O1a1	11b302		lxi	d,datmsg
	01a4	CD3102		call	print	;data Prompt
	01a7	OE7F		mvi	c,127	+;iUP to 127 characters
	01a9	218000		lxi	h,buff	;destination
rloop1:  +;iread next character to buff
OlaC C5	push	b	+;isave counter
OlaD E5	push	h	+;inext destination
OlaE CD0802	call	getchr	+;icharacter to a
OlbI El	pop	h	+;irestore counter
	01b2 C1	pop	b	+;irestore next to fill
	01b3 FEOD	cpi	cr	+;iend of line?
	01b5 CabE0l	jz	erloop1
;	not end ; store character
	01b8 77	mov	m,a
	01b9 23	inx	h	;next to fill
Olba OD	dcr	c	;counter goes down
Olbb C2aCO1	jnz	rloopl	;end of buffer?
CP/M 3 Programmer's Guide	4.3  A Sample Random Access Program
	erloop1 :
;	end of  read loop, Store 00
OlbE 3600	mvi	m,0

;	Write the  record to selected  record number
	Olco	0E28	mvi	c,wrtrzf
	01C2	115C00	lxi	d,fcb
	01C5	CDO5OO	call	bdos
	01C8	b7	ora	a	;error code zero?
	01C9	C2FFOl	jnz	error	;message if not
	O1CC	C34bO1	jmp	ready	+;ifor another record

;***************************************************
	+;i*	*
	+;i* end of Write commands; process read	*
	;*	*
; ***************************************************
notf :
;	not a Write command, read record?
OICF FES2	cpi	'R'
OlDi C2FFO1	jnz	error	;skip if not
	;	read	random record
	01D4	0E21	mvi	c;readr
	01DB	115C00	lxi	d,fcb
	O1D9	CDO5OO	call	bdos
	OlDC	b7	ora	a         +;ireturn code O0?
	OIDD	C2FFOI	jnz	error
	;	read	was Successful , Write to console
OlEO	CD1502	call	crlf	;new line
01E3 0E80	mvi       c,128    ;max 128 characters
OIE5 218000	lxi       h,buff   +;inext to get
Wl oop :
01E8 7E	mov      a,m       ;next character
01E9 23	inx      h         ;next to get
OlEa E67F	ani      7fh       ;mask Parity
OlEC Ca4bOl	jz        ready    ;for another command if 00
OlEF C5	push     b         +;isave counter
OlFO E5	push     h         +;isave next to get
OlFl FEZO	cpi       '  '       ;graphic?
01F3 D40E02	cnc      putchr   +;iskip output if not
OlFB El	pop      h
01F7 C1	pop      b
01F8 OD	dcr      c         +;icount=count-l
01F9 C2E801	jnz      wloop
OlFC C34bO1	jmp       ready
4.3	A Sample Random Access Program	CP/M 3 Programmer's Guide



;***************************************************
	;*	*
	+;i* end of  read command , all errors end-up here	*
	;*	*
	;***************************************************
			e r ro r :
	01FF	11bFO2	lxi 	d,errmsg
	0202	CD3102	call	Print
	0205	C34bO1	jmp 	ready

;***************************************************
	;*	*
	;* utility subroutines for console i/o	*
	+;i*	*
;***************************************************
			getch r:
		                                              +;iread next 
console character to a
	0208	0E0l	mvi 	c,coninp
	02Oa	CDOSOO	call	bdos
	020D	C9	ret
			putch r:
		                                              +;iwrite character 
from a to console
	020E	0E02	mvi 	c,conout
	0210	5F	mov 	e ,a 	;character to Send
	0211	CDO5OO	call	bdos	+;isend character
	0214	C9	ret
			crlf
		                                              
+;isend		carriage	return line feed
	0215	3EOD	mvi 	a,cr 	 ;carriage  return
	0217	CDOEO2	call	putchr
	021a	3EOa	mvi 	a,lf 	 ;line feed
	021C	CDOEO2	call	putchr
	021F	C9	ret
			parse :
		                                              +;iread and parse 
filespec
	0220	11F102	lxi 	d,conbuf
	0223	OEOa	mvi 	c,rstring
	0225	CDO5OO	call	bdos
	0228	111303	lxi 	d,pfncb
	022b	0E98	mvi 	c,parsef
	022D	CDO5OO	call	bdos
	0230	C9	ret
CP/M 3 Programmer's Guide	4.3  A Samp1e Random Access Program
	print:
+;iprint the buffer addreSsed by de Until $
	0231	D5	push	d
	0232	CD1502	call	crlf
	0235	Dl	pop	d	;neW line
	0236	0E09	mvi	c,pstring
	0238	CDOSOO	call	bdos	;print the String
	023b	C9	ret
			readcom:
				+;iread the next command line to the conbuf
	023C	11D102		lxi	d,prompt
	023F	CD3102		call	print    ;command?
	0242	OEOa		mvi	c,rstring
	0244	11F102		lxi	d,conbuf
	0247	CDO5OO		call	bdos     ;read command line
			;	command	line  iS present, scan it
	024a	OEOO		mvi	c,0       ;Start With 00
	024C	210000		lxi	h,0       ,              0000
	024F	11F302		lxi	d,conlin+;icommand line
	0252	la	readc:	idax	d         +;inext command character
	0253	13		inx	d         +;ito next command position
	0254	b7		ora	a         ;cannot be end of command
	0255	C8		rz
			;	not zero, numeric?
	0256	D630		SUi	'0'
	0258	FEOa		cpi	10        +;icarry if numeric
	025a	D27902		jnc	endrd
4,3  A Samp1e Random Access Program	CP/M 3 Programmer's Guide
			;	add-in	next	digit
	025D	F5		push	 psw
	025E	79		mov	 a,c	      ;valUe in ahl
	025F	29		dad	 h
	0260	8F		adc	 a	      +;i*2
	0261	F5		push	 a	      +;isave value * 2
	0262	E5		puSh	 h
	0263	29		dad	 h	      ;*4
	0264	8F		adc	 a
	0265	29		dad	 h	      ;*8
	0266	8F		adc	 a
	0267	C1		pop	b	      +;i*2 + *8 = *10
	0268	09		dad	 b
	0269	C1		pop	b
	026a	88		adc	 b
	026b	C1		pop	b	      +;i+digit
	026C	48		mov	 c,b
	026D	0600		mvi	 b,0
	026F	09		dad	 b
	0270	CEOO		aci	0
	0272	4F		mov	 c,a
	0273	D25202		jnc	 readc
	0276	C33C02		jmp	 readcom
			end rd :
			;	end of	read	,  restore value in a
	0279	C630		adi	 '0'	      ;command
	027b	FEGI		cpi	 ' a '	      +;itranslate case?
	027D	DB		rc
			;	loWer case ,		mask lower case bits
	027E	E65F		ani	 101$1lllb
	0280	C9		ret		      ;return With value in chl

+;i***************************************************
	;*	*
	;* string data area for console messages	*
	+;i*	*
;***************************************************
		badve r:
	0281	736F727279	db	' Sorry, you need cP/m version 3$'
		nospace :
	02a0	6E6F206469	db	'no directory space$'
		datms g :
	02b3	7479706520	db	'type data: $'
		e r rms g :
	O2bF	6572726F72	db	' error, trY again,$'
		prompt:
	02D1	6E65787420	db	'next command? $'
		en tms g :
	02E0	656E746572	db	'enter filename: $'
CP/M 3 Programmer's Guide	4.3  A Sample Random Access Program
	+;i***************************************************
	;*	*
	;* fixed and variable data area	*
	+;i*	*
	+;i***************************************************
	02F1	21	conbuf:	db	conlen	+;ilength of console bUffer
	02F2		consiz:	ds	1	+;iresulting size after read
	02F3		conlin:	ds	32	+;ilength 32 buffer
	0021	=	conlen	equ	$-consiz
	pfncb :
	0313 F302	dW	conlin
	0315 5C00	dw	fcb
	0317	ds	32	+;i16 level Stack
	stack :
	0337	end


	You could make the following major improvements to this program 
to enhance its
operation. With some work, this program could evolve into a 
simple data base
management system. You could, for example, assume a standard 
record size of 128
bytes, consisting of arbitrary fields within the record. You 
could develop a program
called GETKEY that first reads a sequential file and extracts a 
specific field defined
by the operator. For example, the command

GETKEY NAMES.DAT  LASTNAME tO 20

would cause GETKEY to read the data base file NAMES.DAT and 
extract the
"LASTNAME" field from each record, starting at position 10 and 
ending at charac-
ter 20. GETKEY builds a table in memory consisting of each 
particular LASTNAME
field, along with its 16-bit record number location within the 
file. The GETKEY
program then sorts this list and writes a new file, called 
LASTNAME.KEY. This list,
sometimes called an inverted index, is an alphabetical list of 
LASTNAME fields with
their corresponding record numbers.

	You could rename the program shown above to QUERY, and modify it 
so that it
reads a sorted key file into memory. The command line might 
appear as

QUERY NAMES.DAT LASTNAME.KEY
