UMBC CMSC 211

UMBC | CSEE


Basic Operations

Since all devices can be treated as sequential files, including the keyboard and console screen, all read and write operations are handled exactly the same. No special code needs to be written to handle special cases. The discussion will appear to focus on disk files, but it does not matter. One thing to keep in mind, everything that you have done in C can be done in assembly language. Basically, there will be a very similiar operation because everything done in C has to be converted to assembly language anyway!

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 
  
BufferOffset is the location to transfer data to (Read) or from (Write). ByteCount represents the maximum number of byte read or the number of bytes to write. Remember on a Read, you can get less bytes than asked for if you reach the end of the data. A Write should always write the requested number of bytes or it is an error.

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.

Sample Skeleton Program for Opening Or Creating Files

;; 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
  


UMBC | CSEE