| UMBC CMSC 211 |
When a file is opened, it is necessary to keep track of the current position. This represents the number of bytes read or written in a sequential file.
We have two procedures in the library for working with the files that have been opened.
;; _Read Handle, BufferOffset, ByteCount[, BufferSegment] ;; _Write Handle, BufferOffset, ByteCount[, BufferSegment] ;; If successful, each call returns Carry Flag clear ;; and AX equal to the number of bytes read or written ;; End of File (EOF) on _Read is indicated by successful ;; read of 0 bytes
The most efficient buffer size if 512 (or a multiple of 512) bytes, because the physical organization of disk is 512-byte sectors. If you ask for multiple sectors, the operating system will get the data for you.
;; OPENSKEL--Skeleton program to open or create file whose name
;; is first parameter on the command line
;;
;; Program text from "Assembly Language for the IBM PC Family" by
;; William B. Jones, (c) Copyright 1992, 1997, 2001 Scott/Jones Inc.
;;
INCLUDE PCMAC.INC
.MODEL SMALL
.586
.STACK 100h
.DATA
Handle DW ?
Usage DB 'usage: openskel filename .', 13, 10, '$'
AccessMethod EQU 0 ; Or whatever
.CODE
EXTRN ParseCmd : NEAR, CCheck : NEAR
OpenSkel PROC
_Begin
; Did the user provide a filename on the command line
call ParseCmd ; Returns es:si --> file name
test si, si
jnz DoOpen ; Is there a parameter?
_PutStr Usage ; no
_Exit -1
; OK, we have one, use it.
DoOpen:
push ds
_Open si, accessmethod, es ; or _Creat si,es
pop ds
call CCheck ; Abort if error
mov Handle, ax ; Save file handle for later use
; process the open file
;
; ...
;
; Gracefully finish by closing the file.
_Close Handle
; All done!
_Exit 0 ; Normal exit
OpenSkel ENDP
END OpenSkel