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



我最近刚开始操作系统编程,我用nasm编写了一个引导加载程序,它调用内核。。。当我模拟它时,引导程序工作得很好,但内核没有加载,我的代码:

引导程序.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

内核.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

所以如果有人能帮我。。。感谢

您正在做一些假设并跳过一些好的实践。当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

最新更新