flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Help with Bootloader

Author
Thread Post new topic Reply to topic
me239



Joined: 06 Jan 2011
Posts: 200
me239 31 May 2011, 09:11
Hello everyone! Finally after hours of programming, I have my own bootloader. My kernel was booting fine just up until now. I've tested the same bootloader with other sample kernels with no problem, but mine messes it up somehow. It won't even execute my code until the cursor has done a crazy flurry and prints a bunch of gibberish. I've tested my kernel alone with a emulator no problem. PLEASE HELP!
bootloader:
Code:
org 0h
        jmp     start
        nop
        OEM     db      "DOSOPREY"
        BytesPerSector  dw      512
        SectorsPerCluster db    1
        ReservedSectors   dw    1
        NumberofFATs      db    2
        RootEntries       dw    224
        TotalSectors      dw    2880
        Media             db    0xf8
        SectorsPerFAT     dw    9
        SectorsPerTrack   dw    18
        HeadsPerCylinder  dw    2
        HiddenSectors     dd    0
        TotalSectorsBig   dd    0
        DriveNumber       db    0
        Unused            db    0
        BootSig           db    29h
        SerialNumber      dd    0a0a1a2a3h
        VolumeLabel       db    "DOSOFPREY  "
        FileSystem        db    "FAT12   "
start:
        cli
        mov     ax, 0x07c0
        mov     ds, ax
        mov     es, ax
        mov     fs, ax
        mov     gs, ax
        xor     ax, ax
        mov     ss, ax
        mov     sp, 0ffffh
        sti
        mov     ax, 0e31h
        int     10h
        xor     cx, cx
        xor     dx, dx
        mov     ax, 20h
        mul     word[RootEntries]
        div     word[BytesPerSector]
        xchg    cx, ax
        mov     al, byte[NumberofFATs]
        mul     word[SectorsPerFAT]
        add     ax, word[ReservedSectors]
        mov     word[datasector], ax
        add     word[datasector], cx
        mov     bx, 0x0200
        call    ReadSectors
        mov     ax, 0e32h
        int     10h
        mov     cx, word[RootEntries]
        mov     di, 200h
loop1:
        push    cx
        mov     cx, 11
        mov     si, filen
        push    di
        rep     cmpsb
        pop     di
        jz      LoadFat
        add     di, 32d
        loop    loop1
        jmp     fail
LoadFat:
        mov     ax, 0e32h
        int     10h
        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, 200h
        call    ReadSectors
        mov     ax, 0e33h
        int     10h
        mov     ax, 50h
        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, 200h
        add     bx, cx
        mov     dx, word[bx]
        test    ax, 1
        jnz     odd_cluster
even_cluster:
        and     dx, 0000111111111111b
        jmp     done
odd_cluster:
        shr     dx, 4
done:
        mov     word[cluster], dx
        cmp     dx, 0ff0h
        jb      LoadImage
exectute:
        xor     ax, ax
        int     16h
        mov     ax, 0e34h
        int     10h
        push    50h
        push    0
        retf
fail:
        mov     ax, 0e46h
        int     10h
        xor     ax, ax
        int     16h
        hlt
        jmp     $
ReadSectors:
        mov     di, 5
sectloop:
        push    ax
        push    bx
        push    cx
        call    LBACHS
        mov     ah, 02
        mov     al, 1
        mov     ch, byte[absolutetrack]
        mov     cl, byte[absolutesector]
        mov     dh, byte[absolutehead]
        mov     dl, byte[DriveNumber]
        int     13h
        jnc     success
        xor     ax, ax
        int     13h
        dec     di
        pop     cx
        pop     bx
        pop     ax
        jnz     sectloop
        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[absolutesector], dl
        xor     dx, dx
        div     word[HeadsPerCylinder]
        mov     byte[absolutehead], dl
        mov     byte[absolutetrack], al
        ret
absolutesector  db      0
absolutehead    db      0
absolutetrack   db      0
datasector      dw      0
cluster         dw      0
filen           db      "KERNEL  SYS"
times           510-($-$$) db 0
dw 0xAA55
    

Kernel:
Code:
org 0
;mov ax, 0e0dh
;int 10h
;mov al, 0ah
;int 10h
start:
        ;mov     ax, 0003h
        ;int     10h
        xor     ax, ax
        int     16h
        mov     ax, 0x0050
        mov     ds, ax
        mov     es, ax
        mov     fs, ax
        mov     gs, ax
        push    es
        push    0
        pop     es
        cli
        mov     word[es:84h], int21ivt
        mov     word[es:86h], cs
        sti
        ;call    cls
        pop     es
        ;mov     ah, 09h
        ;mov     dx, wmsg
        ;int     21h
main:
        xor     cx, cx
        mov     di, buff
        mov     ax, 0e0dh
        int     10h
        mov     al, 0ah
        int     10h
        mov     dx, prompt
        mov     ah, 09h
        int     21h
kloop:
        xor     ax, ax
        int     16h
        cmp     al, 0dh
        jz      entered
        cmp     al, 08h
        jz      bkspace
        cmp     cx, 50
        jz      kloop
        inc     cx
        stosb
        mov     ah, 0eh
        int     10h
        jmp     kloop
bkspace:
        cmp     cx, 0
        jz      kloop
        mov     ax, 0e08h
        int     10h
        mov     al, 0
        int     10h
        mov     al, 08h
        int     10h
        dec     cx
        jmp     kloop
entered:
        mov     ax, 0e0dh
        int     10h
        mov     al, 0ah
        int     10h
        mov     di, help
        mov     si, buff
        call    cmpstr
        jc      helpprog
        mov     ah, 09h
        mov     dx, msg
        int     21h
        jmp     main
cls:
        push    es
        push    0b800h
        pop     es
        mov     ax, 0700h
        xor     di, di
        mov     cx, 4800
        rep     stosw
        pop     es
helpprog:
        mov     ah, 09h
        mov     dx, helpm
        int     21h
        jmp     main
cmpstr:
        mov     al, [si]
        mov     bl, [di]
        cmp     al, 20h
        je      spexcept
        cmp     al, 61h
        jb      cmpstr_ok
        sub     al, 20h
cmpstr_ok:
        cmp     al, bl
        jne     notequal
        cmp     al, 0
        je      done
        inc     di
        inc     si
        jmp     cmpstr
spexcept:
        inc     di
        mov     al, [di]
        cmp     al, 0
        je      notequal
        ;add     di, 2
        jmp     done
done:
        stc
        ret
notequal:
        clc
        ret
prompt  db      "A:\>$"
wmsg    db      "#@Welcome to DOS OF PREY@#$"
msg     db      "Bad command or filename$"
help    db      "HELP", 0   
helpm   db  "HELP: THIS LIST$"
buff    db      51 dup (0)
int21ivt:
        pushf
        cmp     ah, 09h
        jnz     int_ret
        push    ax bx cx dx
        mov     si, dx
@@:
        lodsb
        cmp     al, 24h
        jz      ah09done
        mov     ah, 0eh
        int     10h
        jmp     @b
ah09done:
        pop     dx cx bx ax
int_ret:
        popf
        iret
    
Post 31 May 2011, 09:11
View user's profile Send private message Reply with quote
christiandy



Joined: 03 Mar 2011
Posts: 25
Location: 101
christiandy 31 May 2011, 09:18
I think your kernel's data and code mixed.
so the program execute your kernel's data.
CMIIW
Post 31 May 2011, 09:18
View user's profile Send private message AIM Address Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 676
Location: Mar del Plata
Enko 31 May 2011, 14:13
off main topic


Quote:

wmsg db "#@Welcome to DOS OF PREY@#$"
msg db "Bad command or filename$"
help db "HELP", 0
helpm db "HELP: THIS LIST$


if you are using strings terminated in $... then:
Quote:

help db "HELP", 0

should be "HELP$"
Post 31 May 2011, 14:13
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 31 May 2011, 16:40
Unless you are booting off DOS then int 21h dont exist.
Post 31 May 2011, 16:40
View user's profile Send private message Reply with quote
me239



Joined: 06 Jan 2011
Posts: 200
me239 31 May 2011, 16:46
bitshifter wrote:
Unless you are booting off DOS then int 21h dont exist.
actually, if you read the code, I setup an ivt for 21h that mimics function ah,09.
Post 31 May 2011, 16:46
View user's profile Send private message Reply with quote
me239



Joined: 06 Jan 2011
Posts: 200
me239 31 May 2011, 18:59
One more thing, the gibberish is actually the FAT12 bootloader info
Image
Post 31 May 2011, 18:59
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 31 May 2011, 19:21
Just to begin troubleshooting this, try replacing "mov ax, 0x0050" with "mov cs, ax" in your kernel source. I know, it should be OK the way it is now because you set up ES to 50h when loading the file to memory, but do it anyway.
Post 31 May 2011, 19:21
View user's profile Send private message Reply with quote
me239



Joined: 06 Jan 2011
Posts: 200
me239 01 Jun 2011, 06:09
Thanks everyone! I fixed it(just reprogrammed everything). Now I'm trying to focus on how to execute other programs. If anyone knows some quick ways to convert the default file names to DOS, 11 byte, padded names, please post Smile
Post 01 Jun 2011, 06:09
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 01 Jun 2011, 07:59
Post 01 Jun 2011, 07:59
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 01 Jun 2011, 14:58
LocoDelAssembly wrote:

Just to begin troubleshooting this, try replacing "mov ax, 0x0050" with "mov cs, ax" in your kernel source.
Now is late, but I actually meant "mov ax, cs", not "mov cs, ax".
Post 01 Jun 2011, 14:58
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.