How to search for a file in a FAT12 system in Assembly -
currently, i've been following brokenthorn series on os dev, , i've run bit of problem. right now, part of journey through tutorial, i'm coding part loads in second stage bootloader, unfortunately, code crashes. here portion of code think troublesome:
code:
;browse root directory binary image mov ax, word [bpbrootentries]; load loop counter, bpbrootentries number of entries in fat table mov di, 0x0000 ; because rep cmpsb compares string in es:di ds:si, , es holds 0x7e00 (the location of fat table), decided set di 0x0000 mov cx, 0x000b; eleven character name lea si, [imagename] ;set si memory location of imagename ds:si points imagename .loop: rep cmpsb jz load_fat add di, 32 ; queue next directory entry dec ax cmp ax, 0x0 jne .loop jmp failure this portion of code looks file in fat table. however, not able find it, , crashes. in code, imagename variable value "krnldr sys" in it. in floppy drive, have file called "krnldr sys" in floppy drive (with spaces, not "krnldr.sys"). great if offer advice.
note: i'm runnning 64-bit windows 7 pc
update
after helpful comments, have updated code:
mov ax, word [bpbrootentries] ; load loop counter mov di, 0x0000 ; locate first root entry mov cx, 0x000b ; eleven character name lea si, [imagename] ; image name find .loop: push di push si repe cmpsb pop di pop si jz load_fat add di, 32 ; queue next directory entry dec ax or ax, ax jne .loop jmp failure unfortunately, os still not able find file.
update 2
here code i've used load root directory table:
load_root: ; compute size of root directory , store in "cx" xor si, si mov ax, 0x0020 ; 32 byte directory entry mul word [bpbrootentries] ; total size of directory div word [bpbbytespersector] ; sectors used directory xchg ax, cx ; compute location of root directory , store in "ax" mov al, byte [bpbnumberoffats] ; number of fats mul word [bpbsectorsperfat] ; sectors used fats add ax, word [bpbreservedsectors] ; adjust bootsector mov word [datasector], ax ; base of root directory add word [datasector], cx ; read root directory memory (7c00:0200) mov dx, 0x7e00 mov es, dx mov bx, 0x0 ; copy root dir above bootcode call readsectors thanks!
the rep on cmpsb repeat cmpsb cx times regardless , zf set result of last comparison made.
- you need
repehere
even repe, repe cmpsb modify both di , si each point byte after last byte compared (let's assume df set up) hence adding 32 di certainly won't point next fat entry.
- you need
pushbothdi,sibeforerepe cmpsb,popthem after (which won't affectflags)
that way, neither di nor si appears changed , adding of 32 valid.
by way of finesse, or ax,ax set zf same cmp ax,0x0 smaller, faster instruction.
Comments
Post a Comment