FAT12

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

Moderator:Moderators

Post Reply
abuashraf
Posts:7
Joined:Sat Aug 30, 2008 7:34 am
FAT12

Post by abuashraf » Sat Aug 30, 2008 7:43 am

Hi,
I'm trying to write a real mode os with asm,
now I'm writing a fat12 driver,till now I could
show list of files and directories,and read a text file,but now I'm trying to write a function that
sets an entry in the FAT table to a new value,for example when you write a new file you have to set
an entry in the FAT table to the cluster number of that file,but my function is not working...
Would you please help me,here's my code...

Code: Select all

setcluster:        
        push    cx bx dx
        mov	ax,word [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,[es:0x500]           ;address of fat table
        add     bx, cx                  ;index into FAT
        mov     dx, WORD [bx]           ;read two bytes from FAT
        test    ax, 0x0001
	jnz	.odd
.eve:	
	mov     bx,dx
	and     dx,1111000000000000b
	mov     bx,word[value]
	and     bx,0000111111111111b
	add     dx,bx
	mov     word [bx],dx
	jmp	donexx
.odd:
	mov     bx,dx
        and     dx,0000000000001111b
        mov     bx,word[value]
        shl     bx,4
	add     dx,bx
	mov     word [bx],dx 
donexx:
        pop     dx bx cx
        ret
Please note that "value" holds the new value
Thanx

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

Post by Andyhhp » Sat Aug 30, 2008 6:06 pm

What is not working about it?

Is it not writing the correct value into the FAT?
Are you writing the file data to the correct clusters?
Are you updating the root directory with the file details?

I cant see any glaring problems with your code but updating the FAT is not the only thing you need to do to write a file to disk

Andrew
Image

abuashraf
Posts:7
Joined:Sat Aug 30, 2008 7:34 am

Post by abuashraf » Sun Aug 31, 2008 12:54 am

Is it not writing the correct value into the FAT?
that's right, it is not writing the correct value
into the FAT,and that's because I have somthing wrong in this function,I'm very sure of the other parts of my code..

User avatar
Andrew
Site Admin
Posts:7
Joined:Sat Mar 08, 2008 9:50 pm
Location:USA
Contact:

Post by Andrew » Tue Sep 02, 2008 6:37 am

abuashraf wrote:
Is it not writing the correct value into the FAT?
that's right, it is not writing the correct value
into the FAT,and that's because I have somthing wrong in this function,I'm very sure of the other parts of my code..
Perhaps your values are off by a decimal? maybe try breaking the lines down one by one into an output and view what values u are getting? im not to familiar with c++ , im more involved in html and java but common mistakes do get the best of us all. also double check the out put maybe your calling on a wrong script? ie. output.a.outputline=(a); but infact its calling output.a.outputline=a;
again i dont kno much=] just trying to lend a hand.
~ Andrew
Lead Design / Lead Tester / Admin
Broken Thorn Ent.

abuashraf
Posts:7
Joined:Sat Aug 30, 2008 7:34 am

Post by abuashraf » Thu Sep 04, 2008 2:04 am

Thanx Andrew,problem has been solved :D ,this
the working code for anybody else...

Code: Select all

setcluster:
        push     cx 
        mov     cx, ax                  ;copy current cluster 
        mov     dx, ax                  ;copy current cluster 
        shr     dx, 0x0001              ;divide by two 
        add     cx, dx                  ;cx=offset into FAT                      
        mov     bx,[es:0x500]           ;address of fat table 
        add     bx, cx                  ;index into FAT
        push    bx 
        mov     dx, WORD [bx]           ;read two bytes from FAT(old value in dx) 
        test    ax, 0x0001 
        jnz     .odd 
.eve:    
        mov     bx,dx 
        and     bx,0xF000               ;bx holds the old value  
        and     word[value],0x0FFF      ;next cluster 
         
        or      word[value],bx 
        mov     dx,word[value]
        pop     bx 
        mov     word [bx],dx 
        jmp     donexx 
.odd: 
        mov     bx,dx 
        and     bx,0x000F               ;bx holds the old value 
        shl     word[value],4           ;next_cluster 
        or      word[value],bx 
        mov     dx,word[value] 
        pop     bx
        mov     word [bx],dx            ;save the new value 
donexx:          
        pop      cx  
Also this is an asm code...
Thanx again.

Post Reply