目標: 學習OS開機流程
閱讀者具備:
1. 組合語言
軟體工具: (all free)
NASM: 2.09.10 refer: http://www.nasm.us/
download: http://www.nasm.us/pub/nasm/releasebuilds/2.09.10/win32/nasm-2.09.10-installer.exe
VMware player: refer http://www.vmware.com/
step1: register (free)
https://www.vmware.com/tryvmware/index.php
step2: downlaod
Knowledge introduction:
開機流程:
power on -> BIOS -> bootloader -> OS
示意圖
programming:
design by notepad or other edit tool.
//-------------------------bootloader.txt------------------------------------------------------------------
%define os_start 0x1000
%define file_start 0x2000
%define driver 0x80
%define os_sector 3
%define file_sector 2
[bits 16]
org 0x7c00
start:
mov ax,cs
mov ds,ax
mov es,ax
mov al,03h
mov ah,0
int 10h ;set screen mode, 80x25
;-------------------------------------
;local service
mov si,msg_hello ;hello message
call print
mov ah,0 ;wait for keypress
int 16h
;-------------------------------------
;load os code to ram
mov ax,os_start
mov es,ax
mov cl,os_sector
mov al,2
call load_sector
;-------------------------------------
;load file table to ram
mov ax,file_start
mov es,ax
mov cl,file_sector
mov al,1
call load_sector
;-------------------------------------
jmp $ ; we don't have os. this is infinite loop.
; jmp os_start:000 ; jump to our shell
;-------------------------------------
;load function
load_sector:
mov bx,0
mov dl,driver ;drive
mov dh,0 ;head
mov ch,0 ;track
mov ah,2
int 0x13
jc exception
ret
exception:
mov si,msg_error
call print
mov ah,0
int 16h
int 19h ;reboot
;------------------------------------
;print function
print:
pusha
mov bp,sp
loop_print:
lodsb
or al,al
jz end_print
mov ah,0x0e
mov bx,0
int 10h
jmp loop_print
end_print:
mov sp,bp
popa
ret
;------------------
;data section
msg_hello db "Booting Successful...",10,13,"Hello !!",10,13,"Press any key to continue.",10,13,0
msg_error db "Exception occor at load_section",10,13,0
size equ $ - start
%if size+2 > 512
%error "code is too large for boot sector"
%endif
times (512 - size - 2) db 0
;--------------------
;end for bootloader
dw 0xaa55
//----------------------------------------------------------------------------------------
assembly by NASM
open NASM
在命令提示列使用
NASM -f bin bootloader.txt -o bootlaoder
確認檔案大小為512 byte
//---------------------------------------------------------------------------------------------------
open VMware player
create a new virtual machine
finished!!