MS-DOS Working with Files

This is a continuation of my series of articles on the history of personal computers.

It follows the article on the MS-DOS multi-segment program.

The series starts with my first CP/M article. If you have not read the previous articles, I recommend that you read them first.

MS-DOS 2.0 introduced a new way to access files and new system calls. I am told the original CP/M-compatible calls still exist and still work but they only exist to support programs ported from CP/M.

Recall the BDOS system call list:

You can see an example of those calls in use in my article on CP/M file control blocks.

And note this (very partial) list of MS-DOS system calls that includes the new (in 1982) file system calls (and omits the backwards-compatible old file system calls):

Remember that system call numbers go into the register C on 8 bit machines and AH on 16 bit machines running MS-DOS. The destination register is DE for CP/M and DX for MS-DOS. The lower half of the destination register is E for CP/M and DL for MS-DOS.

Original system calls (CP/M and MS-DOS)

These system calls work with File Control Blocks which are (hopefully) explained in my previous article linked above. The list is for comparison with the newer calls introduced by MS-DOS 2.0.

The FCB address goes into the destination register, drive number for selecting a drive goes into its lower half. Return codes are stored in the source register (HL for CP/M and BX for MS-DOS). All calls loading or storing data refer to the DMA address that can be set with the call that sets the DMA address (see list).

  • 0Dh: reset drives
  • 0Eh: select drive
  • 0Fh: open file or directory
  • 10h: close file or directory
  • 11h: search for first file
  • 12h: search for next file
  • 13h: delete file or directory
  • 14h: read next record (of 128 bytes) into DMA
  • 15h: write next record
  • 16h: create file or directory
  • 17h: rename file
  • 18h: return logged-in drives
  • 19h: return current drive
  • 1Ah: set DMA address

New system calls (MS-DOS 2.0 and above)

These calls use file names rather than File Control Blocks. The destination register points to a zero-limited ASCII file or directory name. File modes are defined in AL. A number of bytes to read or write is stored in CX. File handles are returned to AX and stored in BX for calls referring to them. File attributes are returned in CX.

  • 36h: get free drive space
  • 39h: create directory
  • 3Ah: remove directory
  • 3Bh: set current directory
  • 3Ch: create file
  • 3Dh: open file
  • 3Eh: close file
  • 3Fh: read from file
  • 40h: write to file
  • 41h: delete file
  • 42h: set current file position
  • 43h: set or get file attributes
  • 47h: get current directory

Perhaps it isn't obvious but the MS-DOS 2.0 system calls are much easier to work with then the File Control Block API defined by CP/M.

The following is a program that demonstrates how to create a file, write to it, read from it, and display its contents.


(Copyable text)

As usual, the program does no error checking and does nothing useful.

If you compile it with FASM, it will run under MS-DOS 2.0 or higher and in the DOS emulator included with x86 versions of Windows.

You can theoretically mix and match the old and new calls but I assume it will be complicated. I have also been told that File Control Blocks are no longer supported by MS-DOS running inside the emulator, so perhaps it is good that this program uses the file name system calls instead.

To be continued...

 © Andrew Brehm 2016