Disk I/O Instructions
(11/21/2003)


An Overview of Disk I/O

A.     The difficulty of doing I/O

  1. synchronizing a fast processor with a slow I/O device
  2. converting different representation of data (e.g. ASCII digit to twos complement binary)
  3. security issues
  4. tedious status and error checking makes writing I/O routines tedious and error prone
Definition:  Logical Record = unit of information meaningful to programmer

B.    Sequential Files : sequence of (fixed or variable length) logical records; only current record is accessible
         (accessing record n required accessing previous n-1 records)

  1. virtual read and write : need file "handle" and memory address for “record”; uses "pointer” to current record
  2. virtual rewind instructions
  3. open permits user access to file (i.e create file handle); close breaks connection to file
 C.    Random Access Files : sequence of fixed length records; any record can be accessed given its position from start of file
  1. read and write : need file "handle", memory address,  position of record
  2. Index Sequential File : separate key to position table (index) maintained
D.    Implementation of Virtual I/O Instructions (includes physical layout of disk)
  1. physical setup : sectors/tracks/surfaces (r/w heads);  cylinders : same track across all surfaces
  2. allocation unit = sector or cluster = adjacent sectors
  3. consecutative vs non-consecutive storage allocation units;  non-consecutive allocation requires some sort of file index or linked list system
  4. free list of allocation units maintained
E    Directory Management Instructions
  1. grouping of files in directories
  2. create, delete, rename, change protection operations

MS-DOS  3-1/4 inch Floppy Disk Organization

Hardware Setup :

    80 (40) circular tracks - numbered 0 - 79 (39)

    9  sectors per track (Double Density) numbered 1 - 9
    18 sectors per track (High Density) numbered 1 - 18

        Each sector is 512 bytes = 0.5 Kbytes

    2 heads or sides numbered 0 - 1

    A high-density diskette has 80 x 18 x 2 = 2880 sectors x 0.5 Kbytes/sector = 1440 Kbytes

Logical Setup :

    Boot Sector : Sector 1 - Track 0 - Head 0

        Contains "boot program" and disk characteristics (# of bytes/sector, # of sectors, sector/track, # of heads)

     Note -  A system disk contains 3 files :

          command.com <- resident part of MS-DOS
          ibmbios.com
          ibmdos.dos  ? hidden files - must be first 2 files on disk

    F.A.T. (File Allocation Table) One entry per cluster (1 cluster equals 3 sectors)

                Pointers to clusters containing file. Allows a file to be stored in non-contiguous locations on disk.

    Root Directory 32 byte entries

        filename          8 bytes
        extension         3 bytes
        attribute         1 byte
        reserved         10 bytes
        time              2 bytes
        date              2 bytes
        starting F.A.T.  2 bytes
        file size         4 bytes

        DD diskettes can have 112 entries; HD can have 224

    Data Area (including subdirectories)


A Short List of MS-DOS Int 21h Virtual I/O Interrupt Services

1. Path name to file given as ASCIIZ Strings - strings terminating in Null character (ASCII 0)
2. File Handles (16 bit) - used by MS-DOS to identify file
3. File Pointers  (maintained by operating system) - points to next byte in file
4. Error Return Codes : CF set on error; AX contains code

INT 21h Service 3Ch - Create a File Handle

 Initialize :   AH <- 3Ch
                CX : file attributes
                    00 - normal attributes
                    01 - read only
                    02 - hidden
                    04 - system
                    08 - volume ID name
                    10h - subdirectory name
                    20h - archive
                DS:DX <- pointer to ASCIIZ path name

 Returns:  if CF = 0 then AX = file handle
           if CF = 1 then AX = error code
                    03 - invalid path
                    04 - too many open files
                    05 - access denied
                        (directory full, matching file exists with restrictive attributes)

 Note : If file with same name exits, it is overwritten

INT 21h Service 3Dh - Open a File

        Initialize: AH <- 3Dh
                    AL : Access Code
                        00 - read
                        01 - write
                        02 - read/write
                    DS:DX <- pointer to ASCIIZ path name

        Returns:    if CF = 0 then AX = file handle
                    if CF = 1 then AX = error code
                        01 - invalid function code
                        02 - file not found
                        03 - invalid path
                        04 - too many open files
                        05 - access denied
                        0Ch - invalid access

 INT 21h Service 3Eh - Close a File

        Initialize: AH <- 3Eh
                    BX <- file handle

        Returns:    if CF = 0 success
                    if CF = 1 then AX = error code
                        06 - invalid handle

INT 21h Service 3Fh - Read from a File or Device

        Initialize: AH <- 3Fh
                    BX <- file handle
                    CX <- number of bytes to read
                    DS:DX <- pointer to read buffer

        Returns:    if CF = 0 then AX = number of bytes read
                    if CF = 1 then AX = error code
                        05 - read access denied
                        06 - invalid handle

    Note: Automatically updates File Pointer

 INT 21h Service 40h - Write to a File or Device

        Initialize: AH <- 40h
                    BX <- file handle
                    CX <- number of bytes to write
                    DS:DX <- pointer to write buffer

        Returns:    if CF = 0 then AX = number of bytes written
                    if CF = 1 then AX = error code
                        05 - write access denied
                        06 - invalid handle

 Note: If CF = 0 on return, AX should equal CX; otherwise an error has occurred.

 INT 21h Service 42h - Move File Read/Write Pointer

        Initialize: AH <- 42h
                    AL <- origin of move
                        00 - beginning of file + offset
                        01 - current location of file pointer + offset
                        02 - end of file + offset
                    BX <- file handle
                    CX:DX <- distance to move (in bytes) - offset

        Returns:    if CF = 0 then DX:AX = new pointer location
                    if CF = 1 then AX = error code
                        01 - AL out of range
                        06 - invalid handle


Int 21h DOS Services for Directory Management

    See Services 39h, 3Ah, 3Bh, 41H, 47h, 4Eh, 4Fh, etc.


Other DOS Based Low-Level Disk I/O Interrupts

Absolute Disk I/O: Sectors accessed by relative position where

relative sector number = (track x sectors/track) + (sector - 1)

INT 25h - Absolute Read

 =< 32M: [MS-DOS 3.3 or earlier)

    Initialize: AL <- drive number ( 0 = A, 1 = B, etc)
                CX <- number of sectors to read
                DX <- relative sector number
                DS:BX <- pointer to Disk Transfer Area

    Returns:    if CF = 0 then success
                if CF = 1 then AX = error code

 > 32M: [MS-DOS 4.0+]

    Initialize  AL <- drive number
                BX <- pointer to 10 byte parameter list
                    32 bit dword specifices 1st sector
                    16 bit word specifices number of sectors to read
                    32 bit dword points to DTA
                CX = -1 (indicates extended >32M) format
 

 INT 26h - Absolute Write

        counter part to INT 25h


BIOS Based Disk I/O - INT 13h - handles all Disk I/O in terms of track and sector numbers

Int 13h Service 02 - Read Floppy Disk Sectors

        Initialize  AH <- 02h
                    AL <- number of sectors to read (1 - 9)
                    CH <- track number (0 - 39)
                    CL <- starting sector number (1 - 9)
                    DH <- head number (0 - 1)
                    DL <- drive number (0 - 3)
                    ES:BX <- pointer to disk buffer

 Int 13h Service 03 - Write Floppy Disk Sectors

        Initialize  AH <- 03h
                    AL <- number of sectors to write (1 - 9)
                    CH <- track number (0 - 39)
                    CL <- starting sector number (1 - 9)
                    DH <- head number (0 - 1)
                    DL <- drive number (0 - 3)
                    ES:BX <- pointer to disk buffer


    Return to Comp 255 Homepage