【问题标题】:Loading kernel in a bootsector在引导扇区中加载内核
【发布时间】:2014-01-11 06:43:29
【问题描述】:

我很新,对这个引导加载程序感到困惑。我使用 QEMU 引导加载程序。我在实现如何在 NASM 中加载内核或某些 .asm 文件时遇到问题。我已经实现了我的内核文件,我想将它加载到我的引导扇区文件中。

我只是按照互联网所说的建立引导扇区,然后我想出了这个:

[BITS 16]    
[ORG 0x7C00]

mov [bootdrv], dl   ;put drive number
        ;disable interrupt, set stack, enable interrupt                         
cli                     
mov ax, 0x9000          
mov ss, ax              
mov sp, 0               
sti         

...
*some code here nothing to do with loading
...

.load_kernel:       
    call read_kernel            ; Load stuff from the bootdrive
    jmp dword KERNEL_SEGMENT:0

read_kernel:
    push ds               
    .reset:
      mov ax, 0               ;reset disk
      mov dl, [bootdrv]       ;drive to reset
      int 13h                
      jc .reset               ;try again if fail
    pop ds

.read:
    *this is where I became confused and lost. I read that you should 
     locate your kernel and pass around using the bootdrv(drive number)
     and return. I can't seem to understand.

任何答案都会很有帮助,因为我真的迷路了。

【问题讨论】:

    标签: kernel nasm bootloader


    【解决方案1】:

    您可以将内核放置在您想要的任何位置。 对您来说最简单的解决方案是用零填充引导加载程序的其余扇区,然后继续在同一个文件中编写代码。

    ; your code
    ...
    
    ; bootloader has 512 bytes, so...
    
    ; fill up to 510 bytes with zeroes and ...
    times 510-($-$$) db 0 
    
    ; place the boot signature on the end
    dw 0xAA55
    

    对于加载,可以使用中断0x13的函数2

    .read:
        push es ; save ES if there's something important in it
    
        ; load the "kernel" at KERNEL_SEGMENT:0
        mov ax, KERNEL_SEGMENT
        mov es, ax
        xor bx, bx
    
        ; your kernel will have 512 bytes (1 sector) for now
        mov al, 0x1
    
        ; get the saved drive number back to DL
        mov dl, [bootdrv]
    
        mov dh, 0x0 ; head 0
        mov cl, 0x2 ; start reading from 2nd sector
    
        ; read
        int 0x13
    
        pop es ; restore ES
    
        ret ; return and continue after call instruction 
    

    【讨论】:

    • 不错,看起来很简单。我可以一点一点地理解。重置已经很好地实现并且堆栈设置得很好。但是,它仍然没有加载我的内核文件。似乎是什么问题?你能教我更多吗?如果您需要更清晰的信息,我可以将我的引导扇区编辑为一个完整的 nasm 代码。
    • 尝试使用一些调试环境。
    猜你喜欢
    • 1970-01-01
    • 2012-04-09
    • 2012-07-25
    • 2017-01-24
    • 2013-08-24
    • 1970-01-01
    • 2016-02-15
    • 2012-02-03
    • 2016-07-02
    相关资源
    最近更新 更多