flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Fails to read more than one cluster :(

Author
Thread Post new topic Reply to topic
me239



Joined: 06 Jan 2011
Posts: 200
me239 17 Jun 2011, 04:54
Hey everyone, so I just got my OS executing programs and ran into a bit of a snag. The kernel can load programs over 1 cluster fine, but fails to repeat it and just hangs. What could be wrong? Here is the code: PS sorry for the large amount of code, but it's straightforward.
Code:
Code:
xor     cx, cx
        xor     dx, dx
        mov     ax, 20h
        mul     word[RootEntries]
        div     word[BytesPerSector]
        xchg    cx, ax
        xor     ax, ax
        mov     al, byte[NumberofFATs]
        mul     word[SectorsPerFAT]
        add     ax, word[ReservedSectors]
        mov     word[datasector], ax
        add     word[datasector], cx
        mov     bx, fatbuff
        call    ReadSectors
    ;pop es
    mov cx, word[RootEntries]
    mov di, fatbuff
file_loop2:
    push  cx
    mov   cx, 11d
    mov si, filen
    push    di
    repe cmpsb
    pop di
    jz load_fat
    pop cx
    add di, 32d
    loop    file_loop2
    jmp     progornot_notprog
load_fat:
        and     byte[di+0bh], 2
        jnz     progornot_notprog
        call    getfat
        mov     bx, fatbuff
        call    ReadSectors
        mov     ax, 1000h
        mov     es, ax
        xor     bx, bx
        push    bx
loadimage:
        mov     ax, word[cluster]
        pop     bx
        call    ClusterLBA
        xor     cx, cx
        mov     cl, byte[SectorsPerCluster]
        cbw
        call    ReadSectors
        push    bx
        mov     ax, word[cluster]
        mov     cx, ax
        mov     dx, ax
        shr     dx, 1
        add     cx, dx
        mov     bx, fatbuff
        add     bx, cx
        mov     dx, word[bx]
        test    ax, 1
        jnz     odd_cluster
even_cluster:
        and     dx, 0000111111111111b
        jmp     exdone
odd_cluster:
        shr     dx, 4
exdone:
        mov     word[cluster], dx
        cmp     dx, 0ff0h
        jb      loadimage
execute:
        mov     ax, 1000h
        cmp     word[es:0], 5a4dh
        jz      RelocateEXE
       mov      ax, es
        sub     ax, 10h                 ; "org 100h" stuff Smile
        mov     es, ax
        mov     ds, ax
        mov     ss, ax
        xor     sp, sp
;**************************************************
        push    0x0010                  ; Poins to int20 function
;**************************************************
        push    es
        push    word 100h
        retf
RelocateEXE:
        mov     ds, ax
        add     ax, [ds:08h]            ; ax = image base
        mov     cx, [ds:06h]            ; cx = reloc items
        mov     bx, [ds:18h]            ; bx = reloc table pointer
        jcxz    RelocationDone
ReloCycle:
        mov     di, [ds:bx]             ; di = item ofs
        mov     dx, [ds:bx+2]           ; dx = item seg (rel)
        add     dx, ax                  ; dx = item seg (abs)
        push    ds
        mov     ds, dx                  ; ds = dx
        add     [ds:di], ax             ; fixup
        pop     ds
        add     bx, 4                   ; point to next entry
        loop    ReloCycle
RelocationDone:
        mov     bx, ax
        add     bx, [ds:0Eh]
        mov     ss, bx                  ; ss for EXE
        mov     sp, [ds:10h]            ; sp for EXE
        add     ax, [ds:16h]            ; cs
        push    ax
        push    word [ds:14h]           ; ip
        retf
progornot_notprog:
        clc
        ret
ReadSectors:
         mov     di, 5
@@:
        push    ax
        push    bx
        push    cx
        call    LBACHS
        mov     ah, 02h
        mov     al, 1
        mov     cl, byte[sector]
        mov     ch, byte[track]
        mov     dl, byte[DriveNumber]
        mov     dh, byte[head]
        int     13h
        jnc     success
        xor     ax, ax
        int     13h
        dec     di
        pop     cx
        pop     bx
        pop     ax
        jnz     @b
        mov     ax, 0e46h
        int     10h
        int     18h
success:
        mov     al, 'S'
        mov     ah, 0eh
        int     10h
        pop     cx bx ax
        add     bx, word[BytesPerSector]
        inc     ax
        loop    ReadSectors
        ret
getfat:
        mov     dx, word[di+1ah]
        mov     word[cluster], dx
        xor     ax, ax
        mov     al, [NumberofFATs]
        mul     word[SectorsPerFAT]
        mov     cx, ax
        mov     ax, word[ReservedSectors]
        ret
ClusterLBA:
        sub     ax, 2
        xor     cx, cx
        mov     cl, byte[SectorsPerCluster]
        mul     cx
        add     ax, word[datasector]
        ret
LBACHS:
        xor     dx, dx
        div     word[SectorsPerTrack]
        inc     dl
        mov     byte[sector], dl
        xor     dx, dx
        div     word[HeadsPerCylinder]
        mov     byte[head], dl
        mov     byte[track], al
        ret
sector  db      0
head    db      0
track   db      0
datasector      dw      0
cluster         dw      0
BytesPerSector  dw      512
        SectorsPerCluster db    1
        ReservedSectors   dw    1
        NumberofFATs      db    2
        RootEntries       dw    224
        SectorsPerFAT     dw    9
        SectorsPerTrack   dw    18
        HeadsPerCylinder  dw    2
        DriveNumber       db    0
    
Post 17 Jun 2011, 04:54
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.