Reputation: 139
Recently I read some nice tutorials about OS development on BrokenThorn : it's a nice OSDev series.
I have some trouble to understand the idea of "computing the next cluster" when I want to load the stage 2 of our bootloader. The code is :
mov ax, WORD [cluster] ; The current Cluster.
mov cx, ax ; copy current cluster
mov dx, ax ; copy current cluster
shr dx, 0x0001 ; divide by two
add cx, dx ; sum for (3/2)
mov bx, 0x0200 ; location of FAT in memory
add bx, cx ; index into FAT
mov dx, WORD [bx] ; read two bytes from FAT
test ax, 0x0001
jnz .ODD_CLUSTER
jz .EVEN_CLUSTER
Why we multiply the current cluster number by 3/2 to get the next cluster number?
Upvotes: 1
Views: 454
Reputation: 319
Remember this is FAT12: each cluster is 12 bits. Because a byte is 8 bits, one entry is one byte and half, or 3/2 bytes!
Upvotes: 1
Reputation: 139
I found The same Question in the assembly Tag, But I can't find it to reference it !! (it's very helpful) Sorry.
So, Why we Multiply the index by 3/2 To Get the index of the Next cluster ??
First of all we need clarify some ideas :
What is an Enrty in the FAT table ??
Is a peace of memory of 12-bits in long.So, FAT table is an ensemble of this Entries.
What there is in this Entries ??
In this peace of Memory there is an index (Number), this index refer to an entry in the same FAT table (how many Entry from the FAT Start address).
Each Entry is 12 bits long :
<---- 12 bits ----><---- 12 bits ----><---- 12 bits ----><---- 12 bits ---->
1st index 2nd index 3rd index 4th index
When we get The first Index from the Root Directory (for Example: 4), So we should Go to This index in The FAT table to find the index for the next cluster number. How we will proceed ??
We need the start address of FAT table (easy to find), And will Just add the index.Like this:
mov bx, [FAT address] ; Strat address of FAT table
mov ax, [cluster] ;the index (for ex: 4)
add bx, ax ;Start address + index = the next Entry (cluster).
Easy :-)!!
But where is the 3/2 fraction ?? :-D .
the above code is Wrong, why ??
The logic is Correct ,but there is a misunderstand of "The addressing unit".
in most CPUs The Unit addressing is BYTE(8-bits), so When we Make [index] that's mean: index * 8-bits in the Memory.
so when we made [cluster] ---> index * 8-bits,Like this we will indexing Entries of 8-bits long.
As against this, Our index is 12-bits not 8-bits !!! So how solve this Big problem lool??
The Solution:
We should just Multiply the [cluster] by 3/2, So :
index * 8bits * 3/2 = index* 12bits. that's what we need !! Now we can indexing the memory By 12-bits unit (1.5 byte).
The correct code is :
mov bx, [FAT address] ; Strat address of FAT table
mov ax, [cluster] ;the index (for ex: 4)
mov dx, ax ; make copy of index
shr dx, 0x0001 ; divide it By 2.
add ax, dx ; index/2 + index = index * 3/2.
add bx, ax ;Start address + index = the next Entry (cluster).
I'm newbie , so if there is something wrong just Notify it ;-D !! Thankx.
Upvotes: 3
Reputation: 5083
This weirdness is specific for FAT12 file systems.
Since size of a record for cluster in FAT12 is 12 bits, in order to compute its offset (in bytes) in the file allocation table you need to take its index and multiply it by 3/2 (or 12bits/8bits).
A cluster record contains number of the next cluster (like pointer in a linked list) of file or end-of-file marks. For free clusters a cluster record may mark bad clusters or just is free.
Upvotes: 1