Page 1 of 1

Help with boot loader/Kernel

Posted: Fri Jul 30, 2010 12:34 am
by me239
I've been trying for weeks now to create a working boot loader that loads another program off the disk. This is a close as I have come to doing so, yet my kernel seems to be failing each time. The prompt won't accept any commands. Here is my code:
Kernel

Code: Select all

org     1000h

;times 512-($-$$) db 0

include 'boot.asm'

;org     1000h

;push    cs

;pop     ds

start:

        push    cs

        pop     ds

        mov     si, hello

        call    printf

main:

        mov     ah, 0eh

        mov     al, '>'

        int     10h

main2:

        push    cs

        pop     ds

        mov     di, buffer

        call    getinput

        mov     si, buffer

        mov     di, cmdhello

        call    cmpstr

        jc      sayhello

        mov     si, buffer

        mov     di, cmdprint

        call    cmpstr

        jc      prints

        mov     si, buffer

        mov     di, cmdhelp

        call    cmpstr

        jc      help

        mov     si, buffer

        mov     di, cmdqt

        call    cmpstr

        jc      quit

        mov     si, buffer

        mov     di, cmdcls

        call    cmpstr

        jc      clearscreen

        mov     si, buffer

        mov     di, cmdec

        call    cmpstr

        jc      echos

        mov     si, buffer

        mov     di, cmdal

        call    cmpstr

        jc      os_msgbox

        mov     si, buffer

        mov     di, cmdrest

        call    cmpstr

        jc      restartpmpt

        cmp     al, 0

        je      main

        mov     si, badcommand

        call    printf

        jmp     main

restartpmpt:

        mov     ah, 0fh

        int     10h

        push    ax bx

        mov     ah, 00

        int     10h

        jmp     start

nomsg:

        mov     si, n2put

        call    printf

        jmp     main

n2put   db      "Syntax: alert <caption>", 0ah, 0dh, 0

os_msgbox:

        lea     si, [buffer+5]

        lodsb

        cmp     al, 0

        je      nomsg

msgbox:

        call    hide_curs

        mov     dl, 19

        mov     dh, 09

mloop:

        call    mv_curs

        pusha

        mov     ah, 09h

        mov     bh, 0

        mov     cx, 42

        mov     bl, 01001111b

        mov     al, ' '

        int     10h

        inc     dh

        cmp     dh, 16

        je      boxdone

        jmp     mloop

boxdone:

        mov     dl, 20

        mov     dh, 10

        call    mv_curs

        xor     cl, cl

printer:

        lodsb

        cmp     al, 0

        je      mcont2

        cmp     cl, 40

        jne     printsl

        mov     dl, 20

        inc     dh

        call    mv_curs

        xor     cl, cl

        ;jmp     printer

printsl:

        mov     ah, 0eh

        int     10h

        inc     cl

        jmp     printer

mcont2:

        mov     bl, 11110000b

        mov     dh, 14

        mov     dl, 35

        mov     si, 10

        mov     di, 15

        call    drawblock

        mov     dl, 39

        mov     dh, 14

        call    mv_curs

        mov     si, okbtn

        call    printf

mouseclick:

        mov     ah, 01h

        int     16h

        jz      m2

        call    keypress

m2:

        ;mov     ax, 0004h

        ;mov     cx, 165h

        ;mov     dx, 78h

        ;int     33h

        mov     ax, 0003h

        int     33h

        cmp     bx, 1

        jne     mouseclick

        cmp     dx, 70h

        jnge    mouseclick

        cmp     cx, 115h

        jnge    mouseclick

        cmp     cx, 165h

        jnle    mouseclick

        cmp     dx, 75h

        jnle    mouseclick

nowait:

        call    show_curs

        jmp     clearscreen

keypress:

        mov     ah, 00h

        int     16h

        cmp     al, 0dh

        je      nowait

        ret

okbtn   db      "OK  ", 0ah, 0ah, 0dh, 0

noinpt:

        mov     si, msginpt

        call    printf

        jmp     main

msginpt db      "Syntax: echo <text>", 0ah, 0dh, 0

echos:

        mov     di, buffer

        lodsb

        cmp     al, 0

        je      noinpt

        mov     ah, 0eh

        int     10h

        mov     al, 0dh

        int     10h

echo2:

        inc     di

        lodsb

        cmp     al, 0

        jne     contec

        mov     ax, 0e0dh

        int     10h

        mov     al, 0ah

        int     10h

        jmp     main

contec:

        mov     ah, 0eh

        int     10h

        jmp     echo2      

nprint:

        mov     si, nprintmsg

        call    printf

        jmp     main

nprintmsg       db      "Syntax: print <text>", 0ah, 0dh, 0

prints:

        lea     si, [buffer+5]

        lodsb

        cmp     al, 0

        je      nprint

        call    printp

sayhello:

        mov     si, msghello

        call    printf

        jmp     main

clearscreen:

        mov     ah, 0fh

        int     10h

        push    ax

        push    bx

        mov     ah, 0

        int     10h

        jmp     main   

help:

        mov     si, msghelp

        call    printf

        jmp     main

cmdhello        db "hello", 0

msgbeta db      "Thanks for using xOS!", 0

cmdec   db      "echo", 0

msghello        db "Hello World!", 0Dh, 0ah, 0

cmdhelp db      "help", 0

cmdal   db      "alert", 0

cmdcls  db      "cls", 0

cmdrest db      "restart", 0

cmdprint        db      "print", 0

pbuff   db      3fh dup (0)

;buglist db      "Bugs: None so far except for the alert command's messagebox can be typed over", 0ah, 0dh, 0

msghelp db      "Commands: ", 0dh, 0ah, "help: this menu", 0dh, 0ah, "hello: hello world", 0dh, 0ah, "print: allows user to type text to first printer", 0dh, 0ah, "quit: exit prompt", 0ah, 0dh, "echo: echo text after command", 0dh, 0ah, "cls: clear screen", 0ah, 0dh, "alert: shows messagebox with text from user input", 0ah, 0dh, "restart: restart prompt", 0ah, 0dh, 0

badcommand      db "Bad command", 0Dh, 0ah, 0

hello   db      "Welcome to xOS beta", 0

cmdqt   db      "quit", 0

buffer  db      40h dup (0)

quit:

        mov     ax, 4c00h

        int     21h

printf:

        lodsb

        cmp     al, 0

        jne     cont

        ret

cont:

        mov     ah, 0eh

        int     10h

        jmp     printf

cmpstr:

        mov     al, [si]

        mov     bl, [di]

        cmp     al, 20h

        je      spexcept

        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

getinput:

push    cs

pop     ds

xor     cl, cl

loops:

        mov     ah, 00h

        int     16h

        cmp     al, 0dh

        je      entered

        cmp     al, 08h

        je      bkspace

        cmp     cl, 3fh

        je      loops

        mov     ah, 0eh

        int     10h

        stosb

        inc     cl

        jmp     loops

bkspace:

        cmp cl, 0       ; beginning of string?

        je  loops       ; yes, ignore the key

 

        dec di

        mov byte [di], 0        ; delete character

        dec cl          ; decrement counter as well

 

        mov ah, 0eh

        mov al, 08h

        int 10h         ; backspace on the screen

 

        mov al, ' '

        int 10h         ; blank character out

 

        mov al, 08h

        int 10h         ; backspace again

 

       jmp  loops      ; go to the main loop 

entered:

        mov     al, 0

        stosb

        mov     ah, 0eh

        mov     al, 0dh

        int     10h

        mov     al, 0ah

        int     10h

        ret

printp:

        mov     ah, 00h

        mov     dx, 00h

ploop:

        lodsb

        cmp     al, 0

        jne     cloop

        ret

cloop:

        int     17h

        jmp     ploop   

mv_curs:

        pusha

        mov     ah, 02h

        mov     bh, 00

        int     10h

        popa

        ret

drawblock:

        pusha

.more:

        call    mv_curs

        mov     ah, 09h

        mov     bh, 0

        mov     cx, si

        mov     al, ' '

        int     10h



        inc     dh



        mov     ax, 0

        mov     al, dh

        cmp     ax, di

        jne     .more

        popa

        ret

hide_curs:

        pusha

        mov     ch, 32

        mov     ah, 1

        mov     al, 3

        int     10h

        popa

        ret

wait_key:

        pusha

        mov     ah, 00h

        int     16h

        cmp     al, 13

        jne     wait_key

        call    show_curs

        popa

        ret      

show_curs:

        pusha

        mov     ch, 6

        mov     cl, 7

        mov     ah, 1

        mov     al, 3

        int     10h

        popa

        ret

os_newline:

        pusha

        mov     ax, 0e0dh

        int     10h

        mov     ax, 0e0ah

        int     10h

        popa    

        ret       

times 1474560-($-$$) db 0

;times   512-($-$$) db (0)
boot loader

Code: Select all

org     7c00h

starts:

        push    cs

        pop     ds

        push    cs

        pop     es

        cli

        mov     ax, 0x0000

        mov     ss, ax

        mov     sp, 0xFFFF

        sti

        call    loads

        jmp     1000h:0

lsuccess        db      "Load successful", 0ah, 0dh, 0

rattempt        db      "Atempting to read drive", 0ah, 0dh, 0

attempt db      "Atempting to load kernel", 0ah, 0dh, 0

rsuccess        db      "Drive successfully restarted", 0ah, 0dh, 0

resttempt       db      "Attempting to restart drive", 0ah, 0dh, 0

bootdrv db      0

ptext:

        lodsb

        cmp     al, 0

        jne     pcont

        retn

pcont:

        mov     ah, 0eh

        int     10h

        jmp     ptext

loads:

        push    ds

.reset:

        mov     si, resttempt

        call    ptext

        mov     ax, 0

        mov     dl, [bootdrv]

        int     13h

        jc      .reset

        pop     ds

        mov     si, rsuccess

        call    ptext

.read:

        mov     si, rattempt

        call    ptext

        mov     ax, 0x1000

        mov     es, ax

        mov     bx, 0

        mov     ah, 2

        mov     al, 5

        mov     cx, 2

        mov     dh, 0

        mov     dl, [bootdrv]

        int     13h

        jc      .read

        mov     si, lsuccess

        call    ptext

        retn

times   512-($-$$) -2 db 0

dw 0aa55h



Re: Help with boot loader/Kernel

Posted: Fri Jul 30, 2010 12:57 am
by Combuster
don't mix ORG statements in a file, if you're using nasm/yasm your assembler probably doesn't treat them like you think they would: only one org statement applies for the whole file. You will need to separate the files and assemble them individually. You can then use the dd command (or an incbin directive in your source)

Apart from that, the macro expansion has the following form (which is broken at any rate, even if you do have an assembler who does behave the way you think it does):

Code: Select all

org 0x1000
; include 'boot.asm'
    org 0x7c00
    ;rest of bootloader code, assembled with origin 0x7c00
; rest of kernel code, assembled starting from origin 0x7e00 (which is neither 0x1000 nor 0x7c00)

Re: Help with boot loader/Kernel

Posted: Fri Jul 30, 2010 1:01 am
by me239
Combuster wrote:don't mix ORG statements in a file, if you're using nasm/yasm your assembler probably doesn't treat them like you think they would: only one org statement applies for the whole file. You will need to separate the files and assemble them individually. You can then use the dd command (or an incbin directive in your source)

Apart from that, the macro expansion has the following form (which is broken at any rate, even if you do have an assembler who does behave the way you think it does):

Code: Select all

org 0x1000
; include 'boot.asm'
    org 0x7c00
    ;rest of bootloader code, assembled with origin 0x7c00
; rest of kernel code, assembled starting from origin 0x7e00 (which is neither 0x1000 nor 0x7c00)
Well i would try that if I could. I have Ubuntu and I don't know how to write to img files since my computer has no floppy drive :(

Re: Help with boot loader/Kernel

Posted: Fri Jul 30, 2010 4:39 am
by Combuster
I recommend that you actually read what I said...

Re: Help with boot loader/Kernel

Posted: Fri Jul 30, 2010 1:12 pm
by me239
Combuster wrote:I recommend that you actually read what I said...
I read what you were saying, I tried dd and it just overwrote the img file. I tried incbin, but FASM doesn't support that instruction. :(

Re: Help with boot loader/Kernel

Posted: Fri Jul 30, 2010 2:10 pm
by egos
Try "file".

Re: Help with boot loader/Kernel

Posted: Fri Jul 30, 2010 2:34 pm
by me239
egos wrote:Try "file".
What is "file"?

Re: Help with boot loader/Kernel

Posted: Fri Jul 30, 2010 2:39 pm
by Combuster
What is a troll?

Re: Help with boot loader/Kernel

Posted: Fri Jul 30, 2010 2:41 pm
by egos
It's fasm directive :@

Code: Select all

  include "boot.asm" ; file "boot.bin",512
  org 0
  ...
  rb 5*512-$ ; times 5*512-$ db 0

Re: Help with boot loader/Kernel

Posted: Fri Jul 30, 2010 2:46 pm
by me239
Combuster wrote:What is a troll?
I tried using the "file" operand in FASM with still no success. I have tried the several techniques below:
1. Writing a file that simply includes the two programs with the "include" - Prompt shows, but won't accept commands
2. Doing the same as above only with the 'file' command - nothing
3. Copying the files with dd - nothing. dd overwrites the other code each time I try to write the 2nd portion
Please help! I know my kernel is fine. I'm starting to think the stack is the problem since my kernel has many calls

Re: Help with boot loader/Kernel

Posted: Fri Jul 30, 2010 2:50 pm
by me239
egos wrote:It's fasm directive :@

Code: Select all

  include "boot.asm" ; file "boot.bin",512
  org 0
  ...
  rb 5*512-$ ; times 5*512-$ db 0
THANK YOU SO MUCH!!! It finally works now! Maybe I'll post some screen shots later. :mrgreen:

Re: Help with boot loader/Kernel

Posted: Fri Jul 30, 2010 3:17 pm
by egos
Try this "boot.asm"

Code: Select all

  org 7C00h
  xor ax,ax
  cli
  mov ss,ax
  mov sp,$$ ; 7C00h. Or you can use 0 instead 0xFFFF.
  sti
  jmp 0:@f
@@:
  mov ds,ax
  mov es,ax
  call loads
  jmp 1000h:0
  ...

Re: Help with boot loader/Kernel

Posted: Fri Jul 30, 2010 11:05 pm
by me239
Thanks for all the help guys! Here is a short little video of it in action on VirtualBox. Thanks again for the advice. :mrgreen:
http://www.youtube.com/watch?v=Xf57b8o2ZOw