16 bit Assembly
Using Register Addressing
Instructions and Op Codes
Overview of my Compiler
16 bit Date Extraction
FAT12 Floppy Disk Access
32 bit to 16 bit File Size
Finding Free Disk Space
16bit Memory Routines
Mouse Callback
Floppy Disk Parameters
Registers
The Root Directory
Using the Stack
Creating a Boot Disk
Direct To Screen Output
16 bit Time Extraction
Basic Display Code
You may have noticed a small problem with the Floppy Disk maths, that don't add up.
On a 1.44 Mb floopy disk there are 2 reading heads
18 sectors per cylinder
80 cylinders
2 * 18 * 80 = 2880 the maximum number of file sectors on the disk.The BOOT, FAT and Root Directory occupy 33 of these sectors so that leaves 2,847 available for files.
The FAT is in 9 sectors so 9 * 512 bytes = 4,608 bytes.
The FAT12 format stores the block numbers as 2 blocks per 3 bytes.
So 4,608 / 3 = 1,536 * 2 blocks each = 3,572 block entries can be written in the FAT.
That means there are 3,572 - 2,847 = 1,025 more entries in the FAT than there are sectors.
Some of these extra entries allow for "File End" markers which do nothing more than fill an entry.
But even with the maximum 224 directory entries it still leaves 801 spare entries.
This means you cannot calculate free space by scanning the FAT.
This image shows the effect of scanning the FAT, the correct number of free spaces was actually 50.
However files saved on a newly formatted disk with no deleted files can be counted correctly, hence 2,847 available sectors minus used sectors = free space.
Deleted Files
This is all fine until a file is deleted. Deleted files do not seem to get deleted at all. Only the name is changed to hide it from the directory listing, which is good if you need to retrieve it.
This can however cause the FAT to look full even when every file has been deleted, because nothing has been cleared. Operating Systems are obviously working around this issue.
So the FAT is not reliable for calculating free space, and a nightmare when trying to find an empty space to save a new file.
It is difficult to calculate Free space from file sizes, because they are in 32 bit numbers and the program is 16 bit.
You could round the file size up to the nearest 1 Kb, which can be done fairly easily in 16 bit. This would be a fair estimate of free space, although not accurate.
One way around this could be to physically count the number of sectors attached to each file on the disk. This is not as difficult as it sounds and will only take a second or two at most once the FAT and Root Directory are loaded into memory.
The biggest problems will arrise when you try to save a file on the disk.
Possible Solutions
You may decided to write a routine that properly removes the deleted file data from the disk.
Identify all the used sectors first be tracking every valid file cluster.
This will make it easier to see free space and find empty blocks for a new file.
The same applies to the Root Directory for the new filename.
Another alternative is to create a mini sector map using one bit to represent each sector, 1 for a used sector, 0 for a free sector. The total data size would be 360 bytes, less than one sector. The data could be saved in a specific sector and marked in the FAT as protected.