IBM PC Assembly Language
____________________________________________________________________________________
Chapter 17
Disk Storage II: Writing and Reading
Files
This chapter covers the use of file handles
and the operations for writing and reading disk
files sequentially and randomly.
http://www.arl.wustl.edu/~lockwood/class/cs30
6/books/artofasm/Chapter_13/CH13-6.html
IBM PC Assembly Language
____________________________________________________________________________________
Functions for INT 21H Disk Handling:
3CH = Create File
3DH = Open File
3EH = Close File
3FH = Read Record
40H = Write Record
42H = Move File Pointer
IBM PC Assembly Language
____________________________________________________________________________________
We will look at these functions by studying
the following programs.
The first program creates a file and then fills
it with names that the user keys in.
The second program reads the names from
the file created by the first program and
prints them on the screen.
IBM PC Assembly Language
____________________________________________________________________________________
• TITLE A17CRFIL (EXE) Create disk file of names
• .MODEL SMALL
• .STACK 64
• .DATA
• NAMEPAR LABEL BYTE ;Parameter list:
• MAXLEN DB 30 ;Maximum length
• NAMELEN DB ? ;Actual length
• NAMEREC DB 30 DUP(' '), 0DH, 0AH ;Entered name,
• ; CR/LF for writing
• ERRCODE DB 00 ;Error indicator
• FILEHAND DW ? ;File handle
• PATHNAME DB 'I:\NAMEFILE.DAT', 0
• PROMPT DB 'Name? '
• OPENMSG DB '*** Open error ***'
• WRITEMSG DB '*** Write error ***'
• ROW DB 0 ;Screen row
• .386 ; ---------------------------------------------
• .CODE
IBM PC Assembly Language
____________________________________________________________________________________
• .CODE
• A10MAIN PROC FAR
• MOV AX,@data ;Initialize
• MOV DS,AX ; segment
• MOV ES,AX ; registers
• MOV AX,0003H ;Set video mode
• INT 10H ; and clear screen
• CALL B10CREATE ;Create file
• CMP ERRCODE,00 ;Create caused error?
• JNZ A90 ; yes, exit
• A20: CALL C10PROC ;Get data
• CMP NAMELEN,00 ;End of input?
• JNE A20 ; no, continue
• CALL E10CLOSE ; yes, close,
• A90: MOV AX,4C00H ;End processing
• INT 21H
• A10MAIN ENDP
IBM PC Assembly Language
____________________________________________________________________________________
• ; Create disk file, test if valid:
• ; -------------------------------
• B10CREATE PROC NEAR ;Uses AX, BP, CX, DX
• MOV AH,3CH ;Request create
• MOV CX,00 ;Normal file
• LEA DX,PATHNAME
• INT 21H
• JC B20 ;Error?
• MOV FILEHAND,AX ; no, save handle
• JMP B90
• B20: LEA BP,OPENMSG ; yes, error,
• MOV CX,19 ; length of message
• CALL F10DISPLY
• MOV ERRCODE,01 ;Set error code
• B90: RET
• B10CREATE ENDP
IBM PC Assembly Language
____________________________________________________________________________________
• ; Accept name from keyboard:
• ; -------------------------
• C10PROC PROC NEAR ;Uses AX, BP, CX, DI, DX
• MOV CX,06 ;Length of prompt
• LEA BP,PROMPT ;Display prompt
• CALL F10DISPLY
• MOV AH,0AH ;Request input
• LEA DX,NAMEPAR ;Accept name
• INT 21H
• CMP NAMELEN,00 ;Is there a name?
• JE C90 ; no, exit
• MOV AL,20H ;Blank for storing
• MOVZX CX,NAMELEN ;Length
• LEA DI,NAMEREC
• ADD DI,CX ;Address + length
• NEG CX ;Calculate
• MOVZX DX,MAXLEN ; remaining
• ADD CX,DX ; length
• REP STOSB ;Set to blank
• CALL D10WRITE ;Write disk record
• C90: RET
• C10PROC ENDP
IBM PC Assembly Language
____________________________________________________________________________________
• ; Write disk record, test if valid:
• ; --------------------------------
• D10WRITE PROC NEAR ;Uses AH, BP, BX, CX, DX
• MOV AH,40H ;Request write record
• MOV BX,FILEHAND
• MOVZX CX,MAXLEN ;30 for name plus
• ADD CX,2 ; 2 for CR/LF
• LEA DX,NAMEREC
• INT 21H
• JNC D20 ;Valid write?
• LEA BP,WRITEMSG ; error message,
• MOV CX,19 ; length
• CALL F10DISPLY ; call error routine
• MOV ERRCODE,01 ;Set error code
• MOV NAMELEN,00
• D20: RET
• D10WRITE ENDP
IBM PC Assembly Language
____________________________________________________________________________________
• ; Close disk file:
• ; ---------------
• E10CLOSE PROC NEAR ;Uses AH, BX
• MOV NAMEREC,1AH ;Set EOF mark
• CALL D10WRITE ; at end of file
• MOV AH,3EH ;Request close
• MOV BX,FILEHAND ; disk file
• INT 21H
• RET
• E10CLOSE ENDP
• ; Display data on screen:
• ; ----------------------
• F10DISPLY PROC NEAR ;Uses AX, BX, DX
• MOV AX,1301H ;BP and CX set on entry
• MOV BX,0016H ;Page:attribute
• MOV DH,ROW ;Row
• MOV DL,00 ; and column
• INT 10H
• INC ROW ;Next row
• RET
• F10DISPLY ENDP
• END A10MAIN
• ENDP
• END A10MAIN
IBM PC Assembly Language
____________________________________________________________________________________
• And now for the second program ….
IBM PC Assembly Language
____________________________________________________________________________________
• TITLE A17RDFIL (EXE) Read disk records sequentially,
• ; display on the screen
• .MODEL SMALL
• .STACK 64
• .DATA
• RECD_LEN EQU 32
• ENDCODE DB 00 ;End process indicator
• FILEHAND DW ? ;File handle
• RECAREA DB 32 DUP(' ') ;Record area
• OPENMSG DB '*** Open error ***'
• PATHNAME DB 'I:\NAMEFILE.SRT',0
• READMSG DB '*** Read error ***'
• ROW DB 00
• .386 ;-------------------------------------------------
IBM PC Assembly Language
____________________________________________________________________________________
• .CODE
• A10MAIN PROC FAR
• MOV AX,@data ;Initialize
• MOV DS,AX ; segment
• MOV ES,AX ; registers
• MOV AL,00H ;Clear full
• CALL Q10SCROLL ; screen
• CALL B10OPEN ;Open file
• CMP ENDCODE,00 ;Valid open?
• JNZ A90 ; no, exit
• A20:
• CALL C10READ ;Read disk record
• CMP ENDCODE,00 ;Normal read?
• JNZ A80 ; no, exit
• LEA BP,RECAREA ; yes, display name
• MOV CX,RECD_LEN ; length
• CALL D10DISPLY ;
• JMP A20 ; continue
• A80: MOV AH,3EH ;Request close file
• MOV BX,FILEHAND ;
• INT 21H
• A90: MOV AX,4C00H ;End processing
• INT 21H
• A10MAIN ENDP
IBM PC Assembly Language
____________________________________________________________________________________
• ; Open file, test if valid:
• ; ------------------------
• B10OPEN PROC NEAR ;Uses AX, BP, CX, DX
• MOV AH,3DH ;Request open
• MOV AL,00 ;Normal file
• LEA DX,PATHNAME
• INT 21H
• JC B20 ;Error?
• MOV FILEHAND,AX ; no, save handle
• JMP B90
• B20: LEA BP,OPENMSG ; yes, message
• MOV CX,18 ; length
• CALL D10DISPLY ; error message
• MOV ENDCODE,01 ; yes,
• B90: RET
• B10OPEN ENDP
IBM PC Assembly Language
____________________________________________________________________________________
• ; Read disk record, test if end of file:
• ; -------------------------------------
• C10READ PROC NEAR ;Uses AX, BP, BX, CX, DX
• MOV AH,3FH ;Request read
• MOV BX,FILEHAND
• MOV CX,RECD_LEN ;32 for name and CR/LF
• LEA DX,RECAREA
• INT 21H
• JC C20 ;Error on read?
• CMP AX,00 ;End of file?
• JE C30
• CMP RECAREA,1AH ;EOF marker?
• JE C30 ; yes, exit
• JMP C90
• C20: LEA BP,READMSG ; no, message
• MOV CX,18 ; length
• CALL D10DISPLY ; error message
• C30: MOV ENDCODE,01 ;Force end
• C90: RET
• C10READ ENDP
IBM PC Assembly Language
____________________________________________________________________________________
• ; Display routine, test for bottom of screen:
• ; ------------------------------------------
• D10DISPLY PROC NEAR ;Uses AX, BX, DX
• MOV AX,1301H ;BP and CX set on entry
• MOV BX,0016H ;Page:attribute
• MOV DH,ROW ;Row
• MOV DL,10 ;Column
• INT 10H
• CMP ROW,23 ;Bottom of
screen?
• JAE D80 ; yes, bypass
• INC ROW ; no, increment row
• JMP D90
• D80: MOV AL,01H ;Scroll
• CALL Q10SCROLL ; one line
• D90: RET
• D10DISPLY ENDP
IBM PC Assembly Language
____________________________________________________________________________________
• ; Scroll screen:
• ; -------------
• Q10SCROLL PROC NEAR ;Uses AX, BH, CX, DX
• MOV AH,06H ;AL set on entry
• MOV BH,1EH ;Set attribute
• MOV CX,0000
• MOV DX,184FH ;Request scroll
• INT 10H
• RET
• Q10SCROLL ENDP
• END A10MAIN