Page 1 of 1

Question about FAT12

Posted: Mon Oct 24, 2005 2:07 am
by GenX
Hi everyone,

Ok, I have been on and off playing around with creating my own OS, I am trying to understand how FAT12 works but something doesn't seem right, I am pretty sure I am missing something, but I just want some clarification so that I understand it properly.

The numbers below represent the first 10 bytes of the FAT organized into their 12 bit entries, so 001 is FAT(0), 122 is FAT(1), etc...

001 122 334 455 667 788 99

If I look at a directory entry and get a first cluster number of 2 then I first read the corresponding cluster, this is fine, I understand this but when it comes to getting the next cluster number from the FAT I don't understand something, I will explain that now.

The process to get the next cluster number is as follows:

EntryLocation = (ClusterNumber / 2) + ClusterNumber

Viewing the FAT as an array of bytes I would read a word, the word I would read is in italics in the above numbers

word FatEntry = FAT[EntryLocation]

now I have to adjust this value to get the 12 bits I am after, here is the problem:

If ClusterNumber is even, I am supposed to clear the highest nibble which would leave me with 344, but a ClusterNumber of 2 means I should end up with 334... It seems to me that I should be shifting FatEntry right 4 bits if its even and clear the high 4 bits if its odd which is opposite to what the documentation says...

I hope you can understand this post, I am really confused about this ???

Cheers. :)

Re:Question about FAT12

Posted: Mon Oct 24, 2005 4:17 am
by Candy
GenX wrote: Hi everyone,

Ok, I have been on and off playing around with creating my own OS, I am trying to understand how FAT12 works but something doesn't seem right, I am pretty sure I am missing something, but I just want some clarification so that I understand it properly.

The numbers below represent the first 10 bytes of the FAT organized into their 12 bit entries, so 001 is FAT(0), 122 is FAT(1), etc...

001 122 334 455 667 788 99

If I look at a directory entry and get a first cluster number of 2 then I first read the corresponding cluster, this is fine, I understand this but when it comes to getting the next cluster number from the FAT I don't understand something, I will explain that now.

The process to get the next cluster number is as follows:

EntryLocation = (ClusterNumber / 2) + ClusterNumber

Viewing the FAT as an array of bytes I would read a word, the word I would read is in italics in the above numbers

word FatEntry = FAT[EntryLocation]

now I have to adjust this value to get the 12 bits I am after, here is the problem:

If ClusterNumber is even, I am supposed to clear the highest nibble which would leave me with 344, but a ClusterNumber of 2 means I should end up with 334... It seems to me that I should be shifting FatEntry right 4 bits if its even and clear the high 4 bits if its odd which is opposite to what the documentation says...

I hope you can understand this post, I am really confused about this ???

Cheers. :)

That's little endian done wrong.


The bytes are like:

00 11 22 33 44 55 66 77 88 -> 100 221 433 554 766 887
so, if you want the 1st entry, load a 16-bit word from address 0. That gives you (after little endian conversion) 1100. Since it's even, mask out the upper 4 bits and you got it.

For the second, load byte 1 and 2 as word. That gives 2211. Shift it right by 1 nibble to get 221, which is the right one.

Re:Question about FAT12

Posted: Mon Oct 24, 2005 4:48 am
by GenX
:-[ Now thats embarassing!!

Thanks a bundle. :)

Re:Question about FAT12

Posted: Mon Oct 24, 2005 6:17 am
by Candy
GenX wrote: :-[ Now thats embarassing!!

Thanks a bundle. :)
Don't mind it, most people (including me) mess up little-endian and big-endian stuff regularly.

On a sidenote, try to use name each nibble, it makes it clearer which bits you want. This is for FAT12:

ab cd ef => dab efc

Re:Question about FAT12

Posted: Mon Oct 24, 2005 6:32 am
by Solar
And then there are people who really believe that little-endian is "correct". :-D

Re:Question about FAT12

Posted: Mon Oct 24, 2005 6:34 am
by Candy
Solar wrote: And then there are people who really believe that little-endian is "correct". :-D
Oh yes, most certainly it is. I usually mess up the big endian stuff :)

Re:Question about FAT12

Posted: Mon Oct 24, 2005 6:42 am
by Solar
Ahem... *cough* seethedoc *cough*... :-D

Re:Question about FAT12

Posted: Mon Oct 24, 2005 7:13 am
by Kemp
I have to say that I can never remember which way round it goes, it's usually a case of

1) Try it the way that seems right
2) Check the result and find out it's wrong
3) Flip it backwards

Re:Question about FAT12

Posted: Mon Oct 24, 2005 7:31 am
by GenX
Phew!! I am not alone. ;D
ab cd ef => dab efc
I think i'll write this down and stick it to my monitor... ;)

Thanks everyone.