flat assembler
Message board for the users of flat assembler.

Index > OS Construction > What's wrong with my code?

Author
Thread Post new topic Reply to topic
me239



Joined: 06 Jan 2011
Posts: 200
me239 08 Jun 2011, 09:31
I've been trying to load a secondary program over my kernel, but it seems to crash every time. I can't seem to find the bug. I inserted some checks to see how far it gets, but I still can't find the bug. Can somebody please help?
Code:
progornot:
    ;mov ax, 0003
    ;int 10h
    mov         si, buff
    mov         di, filen
    call        parsefat
    mov         byte[filen+11d], 24h
    mov         ah, 09h
    mov         dx, filen
    int         21h
    jc          progornot_notprog
    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
        ;push    es
        push    100h
        pop     es
        mov     bx, 0x0200
        call    ReadSectors
    ;pop es
    mov cx, word[RootEntries]
    mov di, 0x0200
file_loop:
    push  cx
    mov   cx, 11d
    mov si, filen
    push    di
    rep cmpsb
    pop di
    jz load_fat
    add di, 32d
    loop    file_loop
    jmp     progornot_notprog
load_fat:
        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]
        mov     bx, 0x200
        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]
        call    ReadSectors
        push    bx
        mov     ax, word[cluster]
        mov     cx, ax
        mov     dx, ax
        shr     dx, 1
        add     cx, dx
        mov     bx, 0x200
        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     dl, byte[DriveNumber]
        push    0x1000
        push    0
        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
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 08 Jun 2011, 09:31
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20445
Location: In your JS exploiting you and your system
revolution 09 Jun 2011, 02:36
You didn't include all of your code so we can't test it for you. But here is something to consider: Are you loading data from the disk over your currently executing code? If so then instead you should move your execution code to a safe place and run from there, then load the new data from disc, and then jmp to the new code.
Post 09 Jun 2011, 02:36
View user's profile Send private message Visit poster's website Reply with quote
me239



Joined: 06 Jan 2011
Posts: 200
me239 09 Jun 2011, 04:31
revolution wrote:
You didn't include all of your code so we can't test it for you. But here is something to consider: Are you loading data from the disk over your currently executing code? If so then instead you should move your execution code to a safe place and run from there, then load the new data from disc, and then jmp to the new code.
That's why I loaded it to segment 2000h. My kernel is at 50h so there is plenty of room.
Post 09 Jun 2011, 04:31
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.