在看《x86汇编语言从实模式到保护模式》这本书时,因为我是用fedora进行学习的,所以书上的很多工具是用不了的,只能用另外的方法来创建硬盘了:
nasm mbr.asm
qemu-img create -f qcow disk.img 128M
dd if=mbr of=./disk.img
qemu disk.img
我们知道处理器加电或复位之后,如果硬盘是首选的启动设备,那么,ROM-BIOS将试图读取硬盘的0面0道1扇区。传统上,这就是主引导扇区。
主引导扇区的数据有512字节,ROM-BIOS程序将他加载到逻辑地址0x0000:0x7c00处,也就是物理地址0x07c00处,然后判断它是否有效。
一个有效的主引导扇区,其最后两个字应该是0x55和0xaa。ROM-BIOS程序首先检测这两个标志,如果主引导扇区有效,则跳到0x0000:0x7c00执行。
编写一个硬盘主引导扇区
eg:
mov ax,0xb800
mov es,ax
mov byte[es:0x00],'L'
mov byte[es:0x01],0x07
mov byte[es:0x02],'a'
mov byte[es:0x03],0x07
mov byte[es:0x04],'b'
mov byte[es:0x05],0x07
mov byte[es:0x06],'e'
mov byte[es:0x07],0x07
mov byte[es:0x08],'l'
mov byte[es:0x09],0x07
mov byte[es:0x0a],' '
mov byte[es:0x0b],0x07
mov byte[es:0x0c],'o'
mov byte[es:0x0d],0x07
mov byte[es:0x0e],'f'
mov byte[es:0x0f],0x07
mov byte[es:0x10],'f'
mov byte[es:0x11],0x07
mov byte[es:0x12],'s'
mov byte[es:0x13],0x07
mov byte[es:0x14],'e'
mov byte[es:0x15],0x07
mov byte[es:0x16],'t'
mov byte[es:0x17],0x07
mov byte[es:0x18],':'
mov byte[es:0x19],0x07
mov ax,number
mov bx,10
mov cx,cs
mov ds,cx
mov dx,0
div bx
mov [0x7c00+number+0x00],dl
xor dx,dx
div bx
mov [0x7c00+number+0x01],dl
xor dx,dx
div bx
mov [0x7c00+number+0x02],dl
xor dx,dx
div bx
mov [0x7c00+number+0x03],dl
xor dx,dx
div bx
mov [0x7c00+number+0x04],dl
mov al,[0x7c00+number+0x04]
add al,0x30
mov [es:0x1a],al
mov byte [es:0x1b],0x04
mov al,[0x7c00+number+0x03]
add al,0x30
mov [es:0x1c],al
mov byte [es:0x1d],0x04
mov al,[0x7c00+number+0x02]
add al,0x30
mov [es:0x1e],al
mov byte [es:0x1f],0x04
mov al,[0x7c00+number+0x01]
add al,0x30
mov [es:0x20],al
mov byte [es:0x21],0x04
mov al,[0x7c00+number+0x00]
add al,0x30
mov [es:0x22],al
mov byte [es:0x23],0x04
mov byte [es:0x24],'D'
mov byte [es:0x25],0x04
infi:
jmp near infi
number db 0,0,0,0,0
times 203 db 0
db 0x55,0xaa