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

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

compute next cluster

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

This takes a bit of understanding of binary arithmatic.

Code: Select all

``mov cx, ax ; copy current clustermov 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 mns
Posts: 5
Joined: Sat Aug 07, 2010 9:00 pm

Re: compute next cluster

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

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 mns
Posts: 5
Joined: Sat Aug 07, 2010 9:00 pm

Re: compute next cluster

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

The multiplication by 8 is subtle. It is because you are taking an integer and using it as a byte offset. mns
Posts: 5
Joined: Sat Aug 07, 2010 9:00 pm

Re: compute next cluster

thank you very much Andyhhp .I think I got the point.but still there is lot to read for me to understand every bit Hoozim
Posts: 34
Joined: Sun Nov 21, 2010 6:40 pm

Re: compute next cluster

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

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 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

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