我很难设置 GDT 并切换到保护模式。主要是因为我对线性寻址不够了解。这是我的内核代码(kernel.asm):
jmp main
%include "gdt.inc"
main:
call InstallGDT
cli
mov eax,cr0
or eax,1
mov cr0,eax
jmp 08h:Stage3+0x10000
bits 32
Stage3:
mov ax,0x10
mov ds,ax
mov ss,ax
mov es,ax
mov esp,90000h
mov byte [0xb8000],'v'
cli
hlt
这是gdt.inc:
bits 16
InstallGDT:
cli
pusha
lgdt [toc]
sti
popa
ret
gdt_data:
dd 0
dd 0
dw 0ffffh
dw 0
db 0
db 10011010b
db 11001111b
db 0
dw 0ffffh
dw 0
db 0
db 10010010b
db 11001111b
db 0
end_of_gdt:
toc:
dw end_of_gdt-gdt_data-1
dd gdt_data+0x10000
还有我的引导程序(bootloader.asm):
org 0x7c00
bits 16
mov ax,0x9000
mov ss,ax
mov sp,ax
mov [bootdrive],dl
load1:
mov dl,[bootdrive]
xor ax,ax
int 13h
jc load1
load2:
mov ax,0x1000
mov es,ax
mov bx,0
mov al,1
mov ch,0
mov cl,2
mov dh,0
mov ah,2
mov dl,[bootdrive]
int 13h
jc load2
mov ax,0
mov es,ax
mov ds,ax
mov bp,kernel
mov ah,0x13
mov bh,0
mov al,1
mov bl,0x8
mov cx,18
xor dh,dh
xor dl,dl
int 10h
mov ax,0x1000
mov es,ax
mov ds,ax
jmp 0x1000:0x0000
bootdrive db 0
kernel db "bootloader"
times 510-($-$$) hlt
dw 0xaa55
我正在使用 Ubuntu LTS 14.04 32 位,我使用的命令是:
nasm -f bin -o bootloader.bin bootloader.asm
nasm -f bin -o kernel.bin kernel.asm
cat bootloader.bin kernel.bin>myOS.bin
qemu-system-i386 myOS.bin
程序不断重启。