gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:10:03 25 4
gpt4 key购买 nike

我最近开始从事 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 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

关于kernel - 如何从我的 16 位引导加载程序加载内核,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29337102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com