What's in a DOS
Article Index
What's in a DOS
How it works
Writing a file


So how does a DOS work? The best way to understand is to try to implement a DOS for yourself.

Working with blocks

For simplicity it is better to imagine that every sector has a unique address or block number rather than head, track sector number – after all this is how modern drives work. The simplest way of organising the disk is to keep a list of file names and the blocks that are used to store the data. For example,

MyLetter  01,02,10
Notes 03,15,09

indicates that the file MyLetter is composed of block 1,2 and 10 and Notes is composed of blocks 03,15 and 09. Simple enough but there are a number of unresolved questions in this method. The first is, where is this list to be stored?

The obvious answer is on the disk drive itself. Any disk drive has to use a proportion of the space to store a catalogue of what is on the drive – this data has to be stored somewhere.

The most simple method is to use a fixed area – usually the first few tracks. The problem with this is that a fixed size catalogue can only hold the details of a set number of files. This means that a disk can appear full even though there is storage space left on it - early systems really did give a "Disk Full" message when in reality onlyt the fixed sized directory had been used up.

On the other hand if you reserve too much space for the catalogue and then only store a few large files then much of it will be wasted.

Keeping a list of the file names is only the start of the problem. How are you going to keep track of which blocks are used and which are free? How are you going to deal with the problem that the number of blocks that a file uses is variable and so you need a variable amount of space to record them in? The best way to answer these and other questions is to describe how the world’s favourite operating system does it.


Windows inherits MS-DOS’s filing system FAT – File Allocation Table. Although it has been superseded by the NTFS filing system in most cases it is still a supported option and often used for small and simple storage devices such as USB "drives". NTFS is more complex than FAT and hence doesn't make a good example.

The FAT is a table of data written on the disk drive at a fixed location when it is formatted. In theory the table contains one entry for each block on the disk. When the FAT is first written to the disk every entry is zero and a zero is used to indicate that block is free for use.

As well as the FAT the disk also contains a fixed area – the root directory - that is used to record filenames. Each root directory entry consists of a filename and the number of the first block in the file. As every file has at least one block, a directory entry that is all zeros is free for use. If the file consists of more than one block then the number of the second block is stored in the FAT. Where?

The answer is that it is stored in the entry corresponding to the first block. That is, each entry in the FAT is either zero, indicating that the block is free for use, or it “points” to the next block in the “chain” and the start of the chain is stored in the root directory entry along with the file name. The final block in the chain is marked by the use of the special code FFF8 to FFFF. The code FFF7 is used to indicate that the block is damaged and should not be used.

Reading a file

So to read a file you first find its name, MyLetter say, in the root directory. You then know the number of the first block that it uses to store data – block 05 say. You can now read block 05 and process the data it contains.

When you want to read the next block you go to entry 05 in the FAT and read the block number of the next block in the file 03 say. You can now read and process block 03. When you want to read the next block you look in the FAT in entry 03 to get the next block number.

You carry on like this working your way along the file until the FAT entry is bigger than or equal to  FFF8. At this point you know you have got to the end of the file and there isn’t another block.





RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.