2008年8月15日星期五

通用bootsect

今天我渐渐开始伟大宏伟的os计划, os的名字叫yunxingos, 很特别吧?
下面这个程序的功能是,
众所周知,x86 cpu启动时会调用 f000:fff0 的程序,这是bios的程序,而bios会把boot disk的第一个sector 512 byte读入0x7c0:0000,然后执行, 操作系统就从这里开始。 由于只读了512byte,所以要干接下来的事情,一定要在这里读入更多的磁盘的内容。 所以这里我参考linux1.0的代码,更加简化,修改, 读入指定的4个sector的1204byte的内容,然后执行, 放入的地点是0x9000:0000, 为什么放这里呢, 因为有些内容是被系统的一些东西占用的,而这里的空的,而且我让他和linux相同.
bootsect binary

; derived by neoedmund from
; bootsect.s Copyright (C) 1991, 1992 Linus Torvalds
; modified by Drew Eckhardt
; modified by Bruce Evans (bde)
;
; use fasm to compile to a 512 byte binary
;
; bootsect.s is loaded at 0x7c00 by the bios-startup routines, and read
; SETUPSECS sectors from disk to address 0x90000, and jumps there.


INITSEG equ 0x9000
SETUPSECS equ 4 ; nr of setup-sectors
BOOTSEG equ 0x07C0 ; original address of boot-sector

_main:
mov ax,BOOTSEG
mov ds,ax
mov ax,INITSEG
mov es,ax

load_setup:
; mov dx,[0] ; drive 0, head 0
mov cx,0x0002 ; sector 2, track 0
mov bx,0x0000 ; address = 0
mov ax,0x0200+SETUPSECS ; service 2, nr of sectors
; (assume all on head 0, track 0)
int 0x13 ; read it
jnc ok_load_setup ; ok - continue

push ax ; dump error code
call print_nl
mov bp, sp
call print_hex
die1: jmp die1


ok_load_setup:
; Print some inane message
mov ah,0x03 ; read cursor pos
xor bh,bh
int 0x10

mov cx,9
mov bx,0x0005 ; page 0, attribute 7 (normal)
mov bp,msg1
mov ax,BOOTSEG
mov es,ax
mov ax,0x1301 ; write string, move cursor
int 0x10
call print_nl

; Go to load
jmp far INITSEG:0


print_nl:
mov ax,0xe0d ; CR
int 0x10
mov al,0xa ; LF
int 0x10
ret

;
; print_hex is for debugging purposes, and prints the word
; pointed to by ss:bp in hexadecmial.
;

print_hex:
mov cx,4 ; 4 hex digits
mov dx, [bp] ; load word into dx
print_digit:
rol dx,4 ; rotate so that lowest 4 bits are used
mov ah,0xe
mov al, dl ; mask off so we have only next nibble
and al,0xf
add al,'0' ; convert to 0-based digit
cmp al,'9' ; check for overflow
jbe good_digit
add al,'A' - '0' - 10

good_digit:
int 0x10
loop print_digit
ret

msg1:
db 13,10
db "Loading"

;.org 498
rb 512-2-$
boot_flag:
dw 0xAA55

没有评论:

博客归档

neoedmund's shared items

我的简介

ZIP Code File