TITLE   Crappy demo showing dos file read/write and keyboard input routines
;===========================================================
;Shows basic file and keyboard routines.  It asks the
;user for a filename, then a string then write the string
;in the filename.
;Compile into a .COM file!  Small eh!
;============================================================
.MODEL TINY                    ;Sets it for a tiny memory model
.8086                          ;Use 8086 code for compatability
.CODE
.STARTUP


START:
	MOV     AX,0619h        ; Clear the screen
	MOV     BH,7
	XOR     CX,CX
	MOV     DX,1950h        ;Scroll 25 lines to clear the screen
	INT     10h
	MOV     AX,0200h        ;Put cursor in top left hand corner
	XOR     BX,BX           ;Video Page 0
	XOR     DX,DX           ;Pos 0,0
	INT     10h

	MOV	DX,OFFSET MESSAGE	;Print startup message
	MOV     AH,09h
	INT     21h
	MOV	DX,OFFSET ASKFILE	;Print ask for filename message
	INT     21h

	MOV	SI,OFFSET FILE	       ;Point SI to file variable
	MOV     CX,12           ;No more than 12 characters
INPUT1:
	MOV     AX,0100h
	INT     21h             ;Get user input
	CMP     AL,13
	JE      EINPUT1
	CMP     AL,08
	JNE     INPUT1A
	INC     CX
	DEC     SI
	MOV     BYTE PTR [SI],32
	MOV     AX,0A20h
	XOR     BX,BX
	MOV	DX,CX
	MOV     CX,1
	INT     10h
	MOV	CX,DX
	JMP     INPUT1
INPUT1A:
	MOV     [SI],AL
	INC     SI
	LOOP    INPUT1
EINPUT1:
	MOV     AX,3C00h
	XOR     CX,CX
	MOV	DX,OFFSET FILE
	INT     21h
	JNC     FILEOK

        MOV     DX,OFFSET FILENOTOPEN   ;Could not create file.
	MOV     AH,09h          ;Print error message
	INT     21h
	MOV     AX,4C01h        ;and exit
	INT     21h

FILEOK:
	MOV     FILEHANDLE,AX   ;Save filehandle
	MOV	DX,OFFSET FILEOPEN	;Else were O.K. to proceed
	MOV     AH,09h
	INT     21h
	MOV	DX,OFFSET ASKNAME	 ;Ask for the name
	MOV     AH,09h
	INT     21h

	MOV     CX,60000        ;Get user input
	MOV     AX,3F00h
	MOV     BX,0            ;Read from console
	MOV     DX,4096         ;Point DX to memory location 4096
				;.COM files have all memory allocated by default
				;so this is legal, as it is past all the code
				;but still in the same segment which is
				;pointed to by DS and CS
	INT     21H
	MOV     CX,AX           ;Save amount of bytes read in CX

	MOV     AH,40h          ;Write name to the open file
	MOV     BX,FILEHANDLE   ;using currently open file
	INT     21h             ;Bytes to write has been set by the amout
				;of bytes recieved when user input read
	JNC     WRITEOK
	MOV	DX,OFFSET NOWRITE	;If write unsuccesful, tell the user
	MOV     AH,09h
	INT     21h
	MOV     AH,3Eh          ;Close file
	MOV     BX,FILEHANDLE
	INT     21h
	MOV     AX,4C01h
	INT     21h             ;And exit

WRITEOK:
	MOV     AH,3Eh          ;Close file
	MOV     BX,FILEHANDLE
	INT     21h
	
	MOV	DX,OFFSET WRITEFINE	;Tell user write to disk went well
	MOV     AH,09h
	INT     21h
	
	MOV     AX,4C00h         ; Send exit code to dos
	INT     21h             ; Send command to DOS
	;Put data after code so we dont need to jump over it

MESSAGE         DB      09,09,09,"**** DK SOFT  FILE UTILITY ****",0ah,0dh,24h
FILE            DB      13 DUP (0)
FILEHANDLE      DW      ?
ASKFILE         DB      0Ah,"Please enter a filename : ",24h
FILEOPEN        DB      0AH,0AH,0DH,"File created.",0ah,0dh,24h
FILENOTOPEN     DB      0AH,0AH,0DH,"Could not create file",0Ah,0Dh,24h
ASKNAME         DB      0AH,0DH,"Please type in a message : ",24h
NOWRITE         DB      0AH,0DH,"Could not write data to file",24H
WRITEFINE       DB      0AH,0DH,"Your message has been written to disk!",24H

END		       ; Set end of program
