让我们直接使用硬盘
;主引导程序
;--------------------------------------------------------------------
%include 'boot.inc'
SECTION MBR vstart=0x7c00
mov ax,cs
mov ds,ax
mov es,ax
mov ss,ax
mov fs,ax
mov sp,0x7c00
mov ax,0xb800
mov gs,ax
;--------------------------------------------------------------------
mov ax,0600h
mov bx,0700h
mov cx,0
mov dx,184fh
;--------------------------------------------------------------------
int 10h
;--------------------------------------------------------------------
mov byte [gs:0x00],'1'
mov byte [gs:0x01],0xA4
mov byte [gs:0x02],' '
mov byte [gs:0x03],0xA4
mov byte [gs:0x04],'M'
mov byte [gs:0x05],0xA4
mov byte [gs:0x06],'B'
mov byte [gs:0x07],0xA4
mov byte [gs:0x08],'R'
mov byte [gs:0x09],0xA4
;--------------------------------------------------------------------
mov eax,LOADER_START_SECTOR
mov bx,LOADER_BASE_ADDR
mov cx,1
call rd_disk_m_16
jmp LOADER_BASE_ADDR
rd_disk_m_16:
mov esi,eax
mov di,cx
mov dx,0x1f2
mov al,cl
out dx,al
mov eax,esi
mov dx,0x1f3
out dx,al
mov cl,8
shr eax,cl
mov dx,0x1f4
out dx,al
shr eax,cl
mov dx,0x1f5
out dx,al
shr eax,cl
and al,0x0f
or al,0xe0
mov dx,0x1f6
out dx,al
mov dx,0x1f7
mov al,0x20
out dx,al
.not_ready:
nop
in al,dx
and al,0x88
cmp al,0x08
jnz .not_ready
mov ax,di
mov dx,256
mul dx
mov cx,ax
mov dx,0x1f0
.go_on_read:
in ax,dx
mov [bx],ax
add bx,2
loop .go_on_read
ret
times 510-($-$$) db 0
db 0x55,0xaa
命名为mbr_4.S
然后boot.inc为
;------------------loader and kernel--------------
LOADER_BASE_ADDR equ 0x900
LOADER_START_SECTOR equ 0x2
文件位置如下
[root@makeOS software]# ls
bochs include mbr_3.S mbr_4.S mbr.bin mbr.S
[root@makeOS software]# nasm -I include/ -o mbr.bin mbr_4.S
[root@makeOS software]# dd if=./mbr.bin of=bochs/hd60M.img bs=512 count=1 conv=notrunc
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.0450586 s, 11.4 kB/s
实现内核加载器
%include 'boot.inc'
section loader vstart=LOADER_BASE_ADDR
mov byte [gs:0x00],'2'
mov byte [gs:0x01],0xA4
mov byte [gs:0x02],' '
mov byte [gs:0x03],0xA4
mov byte [gs:0x04],'L'
mov byte [gs:0x05],0xA4
mov byte [gs:0x06],'O'
mov byte [gs:0x07],0xA4
mov byte [gs:0x08],'A'
mov byte [gs:0x09],0xA4
mov byte [gs:0x0a],'D'
mov byte [gs:0x0b],0xA4
mov byte [gs:0x0c],'E'
mov byte [gs:0x0d],0xA4
mov byte [gs:0x0e],'R'
mov byte [gs:0x0f],0xA4
jmp $
[root@makeOS software]# nasm -I include/ -o loader.bin loader.S
[root@makeOS software]# dd if=./loader.bin of=bochs/hd60M.img bs=512 count=1 seek=2 conv=notrunc
0+1 records in
0+1 records out
99 bytes (99 B) copied, 0.000174176 s, 568 kB/s
我们启动虚拟机之后,屏幕上就可以显示” 2 loader “了。