flat assembler
Message board for the users of flat assembler.

Index > OS Construction > reading txt file(memory buffer)...

Author
Thread Post new topic Reply to topic
abuashraf



Joined: 11 Nov 2006
Posts: 88
abuashraf 10 Oct 2008, 05:33
Hi,

I'm trying to read a text file from a fat12 disk,till now I can only
read the first sector of any file,but I cannot get the next cluster of the file
becuase I'm having problem with the memory buffer which I loaded
the fat table into it,

here's my code:
Code:
        mov     ax,word [es:di+26]      ;save starting cluster of the file
                                        ;ax holds the starting cluster of the file
        push    ax es
        mov     bx,0x100
        mov     es,bx
        mov     bx,0x00                 ;load fat table into memory (0x100:0x00)
        mov     ax,1                    ;the starting sector of fat table
        mov     cx,9                    ;fat table occupys 9 sectors
        call    read_sectors
        pop     es ax
  load_file:     
        push    ax                        
        sub     ax,2
        add     ax,33
        mov     bx,0x500                ;file content are loaded at (0x500:0x00)                
        mov     es,bx
        xor     bx,bx
        mov     cx,1                    ;load one sector
        call    read_sectors            ;invoke BIOS
        mov     di,0x00
        mov     cx,512
   reading:
        mov     al,byte [es:di]         ;print the file content
        call    putc
        inc     di
        loop    reading
        call    newline                 ;give me new line
        xor     ah,ah                   ;press any key to continue
        int     16h
        pop     ax
        push    es
        mov     bx,0x100                ;fat table is loaded at (0x100:0x00)
        mov     es,bx       
        call    getcluster              ;get the next cluster of the file 
        pop     es
        cmp     [cluster], 0x0FF0       ;test for end of file
        jg      @.quit                  ;end of file,so just quit
        mov     ax,word[cluster]        ;get the next cluster number   
        jmp     load_file               ;continue printing...
 notfound:                              ;file not found
        call    newline                 ;print new line
        mov     si,nfon                 ;print file not found
        call    print
 @.quit:
        ret      
    


Code:
;-----------------------------------------------;
;returns next cluster of a specific file        ;
;ax=starting cluster                            ;
;returns the cluster number in [cluster]        ;
;assuming FAT is loaded at [100:0x00]           ;
;-----------------------------------------------;      
getcluster:
        push    cx dx bx    
        mov     cx, ax                  ;copy current cluster
        mov     dx, ax                  ;copy current cluster
        xor     di,di
        shr     dx, 0x0001              ;divide by two
        add     cx, dx                  ;sum for (3/2)
        mov     bx,[es:di]              ;address of fat table into memory
        add     bx, cx                  ;index into FAT
        mov     dx, WORD [bx]           ;read two bytes from FAT(dx=old value)
        test    ax, 0x0001
        jnz     .odd_ctr          
 .even_ctr:     
        and     dx, 0000111111111111b    ;take low twelve bits
        jmp     .@.done        
 .odd_ctr:
        shr     dx, 0x0004 
 .@.done:        
        mov     WORD [cluster], dx      ;store new cluster        
        pop     bx dx cx
        ret  
    


I'm pretty sure of my functions,but I think my problem is I'm not reading
the fat table from the right address I loaded it.
I think I'm not passing the right address to BX in "getcluster",
or I'm not loading the fat table into memory in the right way.
would you please help me...

Thanx
Post 10 Oct 2008, 05:33
View user's profile Send private message Reply with quote
nop



Joined: 01 Sep 2008
Posts: 165
Location: right here left there
nop 10 Oct 2008, 09:23
hey abuashraf i can understand this code (i think Very Happy ) looks ok to me but you didnt post all of it

are you running it in dos? i dont think you can cos im pretty sure 0x100:0 and 0x500:0 will be in the dos area unless you load DOS high ?? but there might still be some stuff down there and dos will crash. like 0x3000:0 and 0x4000:0 would be safer if you dont ask dos for the memory

what debugger do you use?
Post 10 Oct 2008, 09:23
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20446
Location: In your JS exploiting you and your system
revolution 10 Oct 2008, 09:45
nop: don't forget that this is the OS Construction section, so DOS is probably not running, one would imagine the OS is likely proprietary.
Post 10 Oct 2008, 09:45
View user's profile Send private message Visit poster's website Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 794
Location: Adelaide
sinsi 10 Oct 2008, 10:01
Code:
        mov     bx,[es:di]              ;address of fat table into memory     

Since ES:DI is always 100:0 you are reading cluster 0 every time (FFF I think)
Code:
        mov     dx, WORD [bx]           ;read two bytes from FAT(dx=old value)     

If you are reading the FAT, you must use [ES:BX]
You really should get used to the ES override you know... Smile

Code:
        cmp     [cluster], 0x0FF0       ;test for end of file    

FF0 to FF6 are reserved, FF7 is a bad cluster, FF8 to FFF is used to mark the last cluster.
Post 10 Oct 2008, 10:01
View user's profile Send private message Reply with quote
abuashraf



Joined: 11 Nov 2006
Posts: 88
abuashraf 10 Oct 2008, 11:14
WoW sinsi,you are amazing, Smile
It's working now ,thank you very much.
Post 10 Oct 2008, 11:14
View user's profile Send private message Reply with quote
nop



Joined: 01 Sep 2008
Posts: 165
Location: right here left there
nop 11 Oct 2008, 03:07
revolution wrote:
nop: don't forget that this is the OS Construction section, so DOS is probably not running, one would imagine the OS is likely proprietary.

good point revolution, im so silly Sad

abuashraf what is your os?

sinsi well spotted that my good friend es: aka 26h was missing
Post 11 Oct 2008, 03:07
View user's profile Send private message Reply with quote
abuashraf



Joined: 11 Nov 2006
Posts: 88
abuashraf 11 Oct 2008, 04:23
Quote:
abuashraf what is your os?

I'm writing my own real mode os,if that answer your question...
Post 11 Oct 2008, 04:23
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.