操作系統實現--保護模式小試

在上一篇的基礎之上,嘗試進入保護模式編程

boot.s用來加載kernel

%define LOAD_KERNEL_TO 0x1000 ;加載到0x1000:0000處執行
%define KERNEL_LEN 20;內核佔據多少個扇區 20*512




org 0x7c00


mov ax,cs
mov ds,ax
mov es,ax




;display"Loading kernel..."


mov ax,msg
mov bp,ax
mov cx,msgLen
mov ax,0x1301
mov bx,0x000c
mov dl,0
int 10h




load:
mov dh,0 ;磁頭號
mov dl,0x00 ;驅動器號
mov ch,0 ;磁道號 
mov cl,2 ;起始扇區號
mov ax,LOAD_KERNEL_TO
mov es,ax ;es:bx -> read data to 
xor bx,bx ;
mov ah,02h ;param
mov al,KERNEL_LEN;how many blocks to read
int 13h


system: 
jmp LOAD_KERNEL_TO:0x00




msg:
db "Loading kernel...."
msgLen equ ($ - msg)


times 510-($-$$) db 0


dw 0xaa55


kernel.s設置進入保護模式

mov ax,cs
mov ds,ax
mov es,ax


mov ax,msg
mov bp,ax
mov cx,msgLen
mov ax,0x1301
mov bx,0x000c
mov dl,0
int 10h
jmp start


msg:
db "The kernel is started..."
msgLen equ ($ - msg)


gdt:
;as編譯器的寫法
; .quad 0x0000000000000000
;gdt_cs: .quad 0x00cf9a000000ffff;cs
;gdt_ds: .quad 0x00cf92000000ffff;ds
;gdt_gs: .quad 0x00c0920b8000ffff;gs


;在nasm好像沒有.quad,自己動手分開手動填充


db 0,0,0,0,0,0,0,0
gdt_cs: db 0xff,0xff,0,0,0,0x9a,0xcf,0
gdt_ds: db 0xff,0xff,0,0,0,0x92,0xcf,0
gdt_gs: db 0xff,0xff,0,0x80,0x0b,0x92,0xc0,0




gdtLen equ $-gdt
gdtptr dw gdtLen - 1;gdt border
dd 0;the base address of gdt,initialized later


selector_cs equ gdt_cs - gdt
selector_ds equ gdt_ds - gdt
selector_gs equ gdt_gs - gdt


start:
mov ax,cs
mov ds,ax
mov es,ax
mov ss,ax


;inital the descriptor of gdt_cs
xor eax,eax
mov ax,cs
shl eax,4
add eax,label_cs
mov word [gdt_cs + 2],ax
shr eax,16
mov byte [gdt_cs + 4],al
mov byte [gdt_cs + 7],ah


;inital the descriptor of gdt_ds
xor eax,eax
mov ax,cs
shl eax,4
add eax,label_ds
mov word [gdt_ds + 2],ax
shr eax,16
mov byte [gdt_ds + 4],al
mov byte [gdt_ds + 7],ah

;inital gdtptr
xor eax,eax
mov ax,cs
shl eax,4
add eax,gdt
mov dword [gdtptr + 2],eax

lgdt [gdtptr]

;close interrupt
cli

;open A20
in al,92h
or al,00000010b
out 92h,al

;switch to protect mode
mov eax,cr0
or eax,1
mov cr0,eax

;jmp to protect mode
jmp dword selector_cs:0


[bits 32];一定要指明是32位!!
label_cs:
mov ax,selector_ds
mov ds,ax
mov ax,selector_gs
mov gs,ax
mov ah,0ch
mov al,[0] ;p
mov [gs:((80*10 + 39) * 2)],ax
mov al,[1] ;r
mov [gs:((80*10 + 39 + 1) * 2)],ax
mov al,[2] ;o
mov [gs:((80*10 + 39 + 2) * 2)],ax
mov al,[3] ;t
mov [gs:((80*10 + 39 + 3) * 2)],ax
mov al,[4] ;e
mov [gs:((80*10 + 39 + 4) * 2)],ax
mov al,[5] ;c
mov [gs:((80*10 + 39 + 5) * 2)],ax
mov al,[6] ;t
mov [gs:((80*10 + 39 + 6) * 2)],ax
mov al,[7] ;
mov [gs:((80*10 + 39 + 7) * 2)],ax
mov al,[8] ;M
mov [gs:((80*10 + 39 + 8) * 2)],ax
mov al,[9] ;o
mov [gs:((80*10 + 39 + 9) * 2)],ax
mov al,[10] ;d
mov [gs:((80*10 + 39 + 10) * 2)],ax
mov al,[11] ;e
mov [gs:((80*10 + 39 + 11) * 2)],ax
jmp $ 


label_ds:


promsg: db "Protect Mode"
promsgLen equ $-promsg


times 10240 - ($-$$) db 0


截一個圖:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章