compute next cluster
Moderator:Moderators
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
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
Re: compute next cluster
This takes a bit of understanding of binary arithmatic.
This means that ax = cx = dx; They are all the same value
This takes dx and divides it by two. dx now equals ax/2 (and by association, cx/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
Code: Select all
mov cx, ax ; copy current cluster
mov dx, ax ; copy current cluster
Code: Select all
shr dx, 0x0001 ; divide by two
Code: Select all
add cx, dx ; sum for (3/2)
Thats how to calculate 3/2cx using divide and add.
~Andrew

Re: compute next cluster
thanx Andyhhp
but thing i want to know is, why we are getting 3/2cx
and why add to bx
but thing i want to know is, why we are getting 3/2cx
and why add to bx

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

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)
but still there is a problem, there is no multiplication by 8 in the code.(please forgive me for my foolishness)
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.

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 


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.
Hopefully this makes sense.
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 !!

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

You can Visit The link below :
http://stackoverflow.com/questions/1384 ... xt-cluster