第2章 编写MBR主引导记录,开始掌权
2.1 计算机启动过程
2.2 第一棒,BIOS
2.2.1 实模式下1MB内存布局
2.2.2 BIOS 何时苏醒
2.2.3 0x7c00
校验启动盘中位于0盘0道1扇区 512 bytes, mbr storage
扇区编号从1开始。
0x55 0xaa 结尾
2.3 让MBR先飞一会儿
2.3.1 神奇的$ $$ 令人迷惑的section
$ 本行代码前的标号
$$ 本section的起始地址
2.3.2 NASM简单用法
提前安装nasm
bochs]$ nasm
bash: nasm: 未找到命令
参考:《操作系统真象还原》第二章 ---- 编写MBR主引导记录 初尝编写的快乐 雏形已显!_编写mbr主引导程序-CSDN博客
sudo yum install nasm
nasm -h
bochs]$ nasm -v
NASM version 2.15.03 compiled on Mar 10 2022
安装完毕
nasm -f <format><filename> [-o <output>]
bin
elf
2.3.3 MBR
c2/a/boot/mbr.S
; main boot program
;------------------------------
SECTION MBR vstart=0x7c00
mov ax,cs
mov ds,ax
mov es,ax
mov ss,ax
mov fs,ax
mov sp,0x7c00
; clear screen, use int 0x06
; ----------------------------
; INT 0x10 function code:0x06 function:clear screen
; ----------------------------
;Input:
;AH function code=0x06
;AL = rows:0
;BH = row attribute?
;(CL,CH)=windows left corner(x,y)
;(DL,DH)=windwos right corner(x,y)
;return value null
mov ax,0x600
mov bx,0x700
mov cx,0
mov dx,0x184f
;VGA text mode:80,25
;from 0:0x18=24,0x4f=79
int 0x10
;;;; 3 rows get cursor ;;;;
;.get_cursor get cursor position, print at cursor
mov ah, 3
mov bh, 0
int 0x10
; dh=cursor row,dl=cursor colum
;;;; get cursor end ;;;;
;;;; print string ;;;;
;int 10h,function code 13
mov ax, message
mov bp, ax
; sreg init
; cursor in dx, cx ignore
mov cx,5
mov ax,0x1301
; al set write mode ah=01:cursor move behind string
mov bx,0x2
; bl char attribute, bl=02h:screen font
int 0x10
;;;; print string end ;;;;
jmp $
message db "1 MBR"
times 510-($-$$) db 0
db 0x55,0xaa
nasm -o mbr.bin mbr.S
ls -lb mbr.bin
512字节
dd if=/media/vdc/emu/bochs/mbr.bin of=/media/vdc/emu/bochs/hd60M.img bs=512 count=1 conv=notrunc
bin/bochs -f bochsrc.disk