3

I recently started in os programming and I've written a bootloader with nasm that calls a kernel... when I emulate it , the bootloader works perfectly but the kernel doesn't load , my code :

bootloader.asm

%define os 0x1000
%define drive 0x80
%define os_sect 3
%define ftable 0x2000
%define ftabsect 2

[BITS 16]
[org 0x7c00]

start:

;boot message
mov si, bootMsg
call printstring


;waitin for user press key
mov ah, 0
int 16h 

;load sector os 
mov ax, os 
mov es, ax 
mov cl, os_sect
mov al, 2
call sectorloader


jmp 0x1000:0



;print character function
printchar:
mov ah, 0x0E
mov bh, 0x00
mov bl, 0x03
int 0x10
ret 

;print string function ~ using printchar function
printstring:
    nextchar:
        mov al, [si]
        inc si
        or al, al 
        jz exit
        call printchar
        jmp nextchar

    exit:
        ret 

sectorloader:
    mov bx, 0
    mov dl, drive
    mov dh, 0
    mov ch, 0
    mov ah, 2
    int 0x13
    jc loaderror
    ret 

loaderror:
    mov si,loadsectorError
    call printstring 
    mov ah,0
    int 16h
    int 19h




;data
bootMsg db 'Booting [OK]',10,13,'Press any key !',10,13,10,13,0
loadsectorError db 'Error while loading sector[!]',10,13,0
TIMES 510 - ($-$$) db 0
DW 0xAA55

kernel.asm

[bits 16]
[org 0]
    mov al, 65
    mov ah, 0x0E
    mov bh, 0x00
    mov bl, 0x03
    int 0x10

I build the programs like so : nasm -f bin -o try.bin bootloader.asm -p kernel.bin

And I emulated it like so : qemu-system-i386 try.bin

So if someone can help me ... thanks

Markian
  • 322
  • 1
  • 12
Le_S
  • 29
  • 5
  • Related: [Michael Petch's general tips for bootloader development](https://stackoverflow.com/questions/32701854/boot-loader-doesnt-jump-to-kernel-code/32705076#32705076), and a working example of [bootloader + kernel](https://stackoverflow.com/questions/33603842/how-to-make-the-kernel-for-my-bootloader/33619597#33619597) that runs on QEMU. – Peter Cordes Dec 15 '17 at 03:07

1 Answers1

-1

You are making some assumptions and skipping some good practices. When BIOS loads your code from the boot sector, the current drive number will be stored in DL. Rather than assuming that it's 0x80 you really ought to store that value. Additionally, it is a very good idea to reset the drive before you try to read that sector. Here is some working code out of one of my boot loaders:

mov     [bootDrive], dl ; DL contains the drive number used to boot.

mov ah,0x00 ; reset disk
int 0x13

            ; Load the next 16 sectors as boot code.
mov dx,0    ; Clear DX
mov ah,0x02 ; read sectors into memory
mov al,0x10 ; number of sectors to read (16)
mov dl,[bootDrive]  ; drive number to reset, previously stored on boot
mov ch,0    ; cylinder number
mov dh,0    ; head number
mov cl,2    ; starting sector number
mov es, 0x1000
mov bx,0    ; address to load to - Ends up being 0x1000:0000
int 0x13    ; call the interrupt routine
jmp 0x1000:0000
David Hoelzer
  • 15,862
  • 4
  • 48
  • 67