You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BIOS 13h中断加载内核失败问题排查求助

制作Bootloader时磁盘读取Kernel出错的问题

运行时触发错误:

Reading from disk failed!

已确认int 13h的寄存器参数符合规范,且Kernel确实被加载到了指定RAM地址,但移除错误处理跳转后,Kernel代码并未执行。在Bochs中调试时,程序确实跳转到了Kernel位置,但看不到输出;QEMU中直接运行则触发磁盘读取错误。

怀疑问题出在构建脚本或段设置上,以下是相关代码:

主文件(bootloader/main.asm)

[bits 16]
[org 0x7c00]

%define ENDL 0x0D, 0x0A, 0

start: jmp boot

%include "bootloader/out.asm"
%include "bootloader/disk.asm"

halt:
  hlt
  jmp halt

boot:
  xor ax, ax ;  sets ax to 0

  ; sets segments to 0
  mov ds, ax
  mov es, ax
  mov ss, ax


  mov si, wlcm_msg
  call print

  mov dh, 1
  mov bx, kernel
  call read_kernel
  jmp kernel

  jmp halt


wlcm_msg: db "Booted to 16 bit real mode", ENDL

times 510-($-$$) db 0
 dw 0xaa55
kernel: db 0

磁盘例程文件(bootloader/disk.asm)

;
; @params:
;   dx(dl) - number of sectors to load
;   bx - loaction in ram to store the read data
;
read_kernel:
  pusha
  push dx

  mov ah, 02h 
  mov al, dh ; sectors to read
  mov cl, 02h ; the sector to read 1 is our bootloader

 mov ch, 0
 mov dl, 0
 mov dh, 0


  int 13h
  jc read_err ; if carry flag is set then there is an error

  pop dx
  cmp al, dh  ; sector read count
  jne sector_err

  popa
  ret

read_err:
  mov si, read_err_msg
  call print
  mov dh, ah
  jmp halt

sector_err:
  mov si, sector_err_msg
  call print
  jmp halt


read_err_msg: db "Reading from disk failed!", ENDL
sector_err_msg: db "Incorrect number of sectors to read!", ENDL

Kernel代码(kernel/main.asm)

[bits 16]

%define ENDL 0x0D, 0x0A, 0

start: jmp main

main:

  mov ah, 9
  xor bh, bh
  mov cx, 1
  mov al, 'E'
  int 10h


  cli 
  hlt

构建脚本(Makefile)

ASM = nasm

BOOT_SRC = bootloader/main.asm
KENREL_SRC = kernel/main.asm
BUILD_DIR = build

.PHONY: all clean create run debug

all: $(BUILD_DIR)/disk.img

$(BUILD_DIR)/disk.img: $(BUILD_DIR)/bootloader.bin $(BUILD_DIR)/kernel.bin
    dd if=/dev/zero of=$@ bs=512 count=2880
    dd if=$< of=$@ bs=512 conv=notrunc seek=0
    dd if=$(word 2,$^) of=$@ bs=512 conv=notrunc seek=1

$(BUILD_DIR)/bootloader.bin: $(BOOT_SRC) create
    $(ASM) -f bin $< -o $@

$(BUILD_DIR)/kernel.bin: $(KENREL_SRC) create
    $(ASM) -f bin $< -o $@

create:
    mkdir -p $(BUILD_DIR)

clean:
    rm -rf $(BUILD_DIR)/*

run: $(BUILD_DIR)/disk.img
    qemu-system-i386 -machine q35 -drive file=$<,format=raw

run_floppy: $(BUILD_DIR)/disk.img
    qemu-system-i386 -machine q35 -fda $<

debug: bochs_config $(BUILD_DIR)/disk.img
    bochs -f $<

尝试多种方法未解决,请求协助定位问题。


内容的提问来源于stack exchange,提问作者Sreyas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 17:05:19