从我的16位引导加载程序加载内核

时间:2015-03-30 01:31:42

标签: kernel nasm bootloader

我最近开始使用os编程,我编写了一个带有nasm的引导加载程序,它调用了一个内核......当我模拟它时,引导加载程序工作正常但是内核没有加载,我的代码:

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 charactere 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

我像这样构建程序: nasm -f bin -o try.bin bootloader.asm -p kernel.bin

我这样仿效: qemu-system-i386 try.bin

所以,如果有人可以帮助我......谢谢

1 个答案:

答案 0 :(得分:0)

你正在做一些假设并跳过一些好的做法。当BIOS从引导扇区加载代码时,当前的驱动器号将存储在DL中。而不是假设它是0x80,你真的应该存储该值。此外,在尝试读取该扇区之前重置驱动器是一个非常好的主意。以下是我的一个启动加载器中的一些工作代码:

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