自己写操作系统1——引导扇区

软件环境:win xp,bochs,nasm,硬盘镜像VHD

这是一个简单的引导程序,因为引导扇区只有512字节,并且最后两个自己必须为55aa所以一共可以利用的字节只有510,但是作为联系应该够了。

这只是一个简单的例子,让引导程序读取虚拟硬盘镜像的三号扇区中的512字节数据到内存0x7e00处。

MBR引导程序

;MBR.ASM
;
;
;
SECTION ALIGN=16 VSTART=0x7C00
LBA_BASE_ADDRESS EQU 5

MOV AX , [cs:SS_BASE]
MOV SS , AX
XOR SP , SP


MOV AX , [cs:DS_BASE]
MOV DS , AX
XOR BX , BX

MOV CX , 512

write_memory:
mov byte [bx] , 0x02
inc bx
loop write_memory
xor bx , bx

CALL WRITE_DISK


JMP $

;
;
;
;DS:BX MEMORY ADDRESS
;
WRITE_DISK:


mov dx , 0x1f2
mov al , 0x01
call out_port
call pio_delay

mov dx , 0x1f3
mov al , 0x03
call out_port
call pio_delay

mov dx , 0x1f4
mov al , 0x00
call out_port
call pio_delay

mov dx , 0x1f5
mov al , 0x00
call out_port
call pio_delay

mov dx , 0x1f6
mov al , 0xe0
call out_port
call pio_delay

call check_drdy

;wait?until?the?controller?is?not?busy
busy_hd:
call read_status
and al , 0x80
jnz busy_hd

;wait?until?the?controller?accepts?command
accept_hd:
call read_status
and al , 0x40
jz accept_hd


mov dx , 0x1f7
mov al , 0x30
call out_port
call pio_delay


;wait until the controller is not busy
busy_hd1:
call read_status
and al , 0x80
jnz busy_hd1

;wait until the controller accepts command
accept_hd2:
call read_status
and al , 0x88
cmp al , 0x08
jnz accept_hd2

mov cx ,   512
mov al , 0x12
mov dx , 0x1f0
s:
call out_port
loop s

mov dx, 0x1f0     ; port
mov di, bx        ; buf
mov cx, 256
cld
rep outsw  

accept_hd3:
call read_status
test al , 0x40
jz accept_hd3

ret

out_port:
out dx , al
ret
;;
check_drdy:
mov dx , 0x1f7
in al, dx
test al, 0x40
jz check_drdy
ret

read_status:
mov dx , 0x1f7
in al , dx
ret
;;
pio_delay:       
nop       
nop       
nop       
nop  
ret


DS_BASE DW 0x1000
SS_BASE DW 0x2000
TARGET_PROGRAM_ADDRESS DB 0x02
TARGET_PROGRAM_SIZE DB 0x01

times 510 - ($-$$) db 0
dw 0xaa55
bochs配置文件:

#Configuration file for Bochs

#how much memory the emulated mchine will have
megs:64

#filename of ROM image
romimage:file=./BIOS-bochs-latest
vgaromimage:file=./VGABIOS-lgpl-latest

#what disk images will be used
#floppya:1_44=./f.img,status=inserted

#hard disk
#ata0-master: type=disk, path="sda.img", mode=flat, cylinders=1015, heads=16, spt=63
ata0-master: type=disk, path="LEECHUNG.vhd", mode=flat, cylinders=1003, heads=12, spt=17

#choose the boot disk
boot:disk

#where do we send log message
log:bochsout.txt

#disable the mouse
mouse:enabled=0

#enable key mapping, using US layout as default.
keyboard_mapping:enabled=1,map=./keymaps/x11-pc-us.map

使用dd命令将mbr.asm程序写入到主引导删除中

dd if=mbr.bin of=XXXXX.vhd bs=512 count=1

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值