根据本教程,创建一个简单的操作系统就足够了,切换到保护模式就像下面的代码一样简单,无需其他众所周知的操作,如启用A20 ...
无论如何,我是这个领域的新手,我写了下面的代码,因为他们提到了这个SO的修改.
代码结构: 这个简单的操作系统应简要加载如下:
但是,模拟器仍在重新启动.请查看随附的整个代码.
bootloader.asm
[bits 16]
[org 0x7C00]
KERNEL_OFFSET equ 0x1000
xor ax, ax
mov ds, ax
mov es, ax
mov [BOOT_DRIVE], dl
mov ax, 0x07E0 ; End of stack
cli
mov ss, ax
mov sp, 0x1200 ; Size of Stack. By this, we assume that stack starts at 9000h
; of size 1200h and ends at 7E00h to avoid being overwritten.
sti
call load_kernel
call switch_to_pm
jmp $ …Run Code Online (Sandbox Code Playgroud) 我正在学习汇编语言.我写了一个简单的bootloader.测试后,它没有用.这是我的代码:
[bits 16]
[org 0x7c00]
jmp start
data:
wolf_wel_msg db 'Welcome to Bootloader!!!',0x0D,0x0A,0
wolf_kernel_load db 'Loading kernel....',0x0D,0x0A,0
wolf_error_msg db 'Kernel.bin not found!',0x0D,0x0A,0
wolf_error_msg1 db 'Press any key to restart..',0
start:
mov si, wolf_wel_msg
call wolf_print
mov si, wolf_kernel_load
call wolf_print
pushf
stc
mov ah,00
mov dl,00
int 13h
read_sector:
mov ax, 0x0
mov es, ax
xor bx, bx
mov ah, 02
mov al, 01
mov ch, 01
mov cl, 02
mov dh, 00
mov dl, 00
int 13h
jc wolf_error …Run Code Online (Sandbox Code Playgroud) 我将引导加载程序从CHS更改为LBA,因此我将其替换int 13h 02h为int 13h 42h.它在QEMU中正常工作,但是,我在使用Bochs和笔记本电脑时遇到了麻烦.
我把bootloader写入了USB闪存盘dd if=main.bin of=/dev/sdb bs=512.笔记本电脑加载英特尔UNDI并给我以下错误:No bootable device - insert boot disk and press any key.
所以我尝试用Bochs调试它,并注意到Bochs将这个二进制文件识别为可引导的.但是,int 13h执行后没有加载任何内容.
然后我尝试从这个闪存驱动器加载我的旧PC,它的工作原理!它加载程序并正确执行它.QEMU给了我相同的结果.
这是引导加载程序代码:
org 0x7c00
bits 16
boot:
cli
; Overlap CS and DS
mov ax, cs
mov ds, ax
mov es, ax
; Setup 4K stack before this bootloader
mov ax, 0x07c0
mov ss, ax
mov sp, 4096
; Load next sectors
mov si, DAP
mov ah, 42h
; …Run Code Online (Sandbox Code Playgroud) 我用汇编写了一个引导加载程序。下面是它的工作原理:
首先,BIOS 正常加载引导加载程序。指示进入200h。
在 200h 处,有一些代码位于 200h 和 21Eh 之间。它只是切换到 VGA 模式,使用 VGA 功能在坐标 1,1 上绘制一个洋红色像素。它永远循环该代码。
然而,当我加载它时,它只是在闪烁的光标上移动,这是普通的 VGA .bin 文件不会做的,并且会显示一个像素。我检查了一个像素,但什么也没看到。我看到的意思是 VGA 代码没有运行,引导加载程序只是加载了,没有别的。
引导加载程序代码:
cli
startload:
push 0x00000200
ret
times 510-($-$$) db 0
db 0x55
db 0xAA
Run Code Online (Sandbox Code Playgroud)
您可以看到它只是转到下一个扇区(从 200h 开始)和 200h-21Eh 处的代码:
BITS 16
org 200h
data:
xcoord DB '1'
ycoord DB '1'
color DB 'D'
.code:
vga:
mov ax, 13h
int 10h
mov ax, ycoord
mov bx, xcoord
mov cx, 320
mul cx
add ax, bx
mov di, ax
mov …Run Code Online (Sandbox Code Playgroud)