compute next cluster

If you are new to OS Development, plan on spending some time here first before going into the other forums.

Moderator:Moderators

Post Reply
mns
Posts:5
Joined:Sat Aug 07, 2010 9:00 pm
compute next cluster

Post by mns » Sat Aug 07, 2010 9:23 pm

i'm still new to the operating system programming.and i'm really gratefull to your article series.
please can some one explain part of the cord below as i found it is very difficult to understand
why divided by two ?
why getting 3/2 value by adding ?
; compute next cluster

mov ax, WORD [cluster] ; identify current cluster from FAT

; is the cluster odd or even? Just divide it by 2 and test!

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

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Re: compute next cluster

Post by Andyhhp » Tue Aug 10, 2010 2:29 pm

This takes a bit of understanding of binary arithmatic.

Code: Select all

mov cx, ax ; copy current cluster
mov dx, ax ; copy current cluster
This means that ax = cx = dx; They are all the same value

Code: Select all

shr dx, 0x0001 ; divide by two
This takes dx and divides it by two. dx now equals ax/2 (and by association, cx/2)

Code: Select all

add cx, dx ; sum for (3/2)
Finally, take dx and add it back onto cx. therefore, we get cx + cx/2 which equals 3/2cx

Thats how to calculate 3/2cx using divide and add.

~Andrew
Image

mns
Posts:5
Joined:Sat Aug 07, 2010 9:00 pm

Re: compute next cluster

Post by mns » Tue Aug 10, 2010 4:23 pm

thanx Andyhhp

but thing i want to know is, why we are getting 3/2cx
and why add to bx :?

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Re: compute next cluster

Post by Andyhhp » Tue Aug 10, 2010 5:07 pm

Thats an implementation detail of FAT12.

In FAT12, each cluster pointer is 12 bits long, but stored in a packed format. Therefore, we get the index which we are looking for, multiply it by 8 to get it in terms of bytes, then multiply by 3/2 to get in terms of 12bits per entry. Then we have the location of the cluster pointer in memory which we can load and work with.

Does that help?

~Andrew
Image

mns
Posts:5
Joined:Sat Aug 07, 2010 9:00 pm

Re: compute next cluster

Post by mns » Sat Aug 21, 2010 4:01 pm

sorry for the delay. thanx Andyhhp
but still there is a problem, there is no multiplication by 8 in the code.(please forgive me for my foolishness)

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Re: compute next cluster

Post by Andyhhp » Sat Aug 21, 2010 11:22 pm

The multiplication by 8 is subtle. It is because you are taking an integer and using it as a byte offset.
Image

mns
Posts:5
Joined:Sat Aug 07, 2010 9:00 pm

Re: compute next cluster

Post by mns » Tue Aug 24, 2010 2:21 pm

thank you very much Andyhhp :D .I think I got the point.but still there is lot to read for me to understand every bit :oops:

Hoozim
Posts:34
Joined:Sun Nov 21, 2010 6:40 pm

Re: compute next cluster

Post by Hoozim » Sun Nov 21, 2010 7:10 pm

Basically, consider each cluster to be 1 byte (because according to the code it is). Each cluster is 1.5 bytes. To get from 1 to 1.5, multiply by 1.5. The only problem is that this involves decimals in which there is no support for. So, by multiplying by three and dividing by two, the same result is achieved without decimals.

Hopefully this makes sense.

smainoo
Posts:2
Joined:Sat Aug 27, 2011 10:49 am

Re: compute next cluster

Post by smainoo » Wed Dec 12, 2012 3:37 pm

Andyhhp wrote:Thats an implementation detail of FAT12.

In FAT12, each cluster pointer is 12 bits long, but stored in a packed format. Therefore, we get the index which we are looking for, multiply it by 8 to get it in terms of bytes, then multiply by 3/2 to get in terms of 12bits per entry. Then we have the location of the cluster pointer in memory which we can load and work with.

Does that help?

~Andrew

Hi Andrew,
Thanks To your Help, But I still Don't Understand :cry:
So, what do You mean with "The index" ?? is it a vlue between "0x002 through 0xFEF" (Next Cluster)??

Can You make an example ??
Thanks & good Programming !! :-)

smainoo
Posts:2
Joined:Sat Aug 27, 2011 10:49 am

Re: compute next cluster

Post by smainoo » Fri Dec 14, 2012 2:47 pm

Hi !! I think I Get The Idea !! :wink:
You can Visit The link below :
http://stackoverflow.com/questions/1384 ... xt-cluster

Post Reply