【来源】<自己动手写操作系统> 于缘
Boot Sector(引导扇区),一个正确的Boot Sector除了以0xAA55结束之外,还应该包含一段少于512B的执行码
源码分析
org 07c00h ;告诉编译器程序加载到7c00处
mov ax,cs
mov ds,ax
mov es,ax
call DispStr ;调用显示字符串例程
jmp $ ;无限循环
DispStr:
mov ax, BootMessage
mov bp, ax ;es:bp = 串地址
mov cx, 16 ;cx = 串长度
mov ax, 01301h ;ah=13,al=01h
mov bx, 000ch ;页号为0(bh=0) 黑底红字(bl=0ch,高亮)
mov dl, 0
int 10h ;10h号中断
ret
BootMessage: db "hello ,OS world!"
times 510-($-$$) db 0 ;填充剩下的空间,使生成的二进制代码恰好为
;512字节
dw 0xaa55 ;结束标记
nasm boot.asm -o boot.bin
硬件自检相关工作完成后,就会启动操作系统:以Windows XP为例,系统BIOS将启动盘的第一扇区(Boot Sector,引导扇区)读入到内存的0x7C00H处,并检查0x7DFE地址的内容是否等于0xAA55,如果匹配,跳转到0x7C00处执行MBR(Master Boot Record,主引导记录),MBR接着从分区表(Partition Table)中找到第一个活动分区(Active Partition ,一般是C盘分区),然后按照类似方式读取并执行这个活动分区的引导扇区(Partition Boot Sector),而引导扇区将负责读取并执行NTLDR(NT LoaDeR,windows nt的加载程序),然后主动权就移交给了windows.(来源<使用开源软件-自己动手写操作系统> 杨文博)
从Xp启动操作系统的过程中可以看出为什么要用org 0x7c00h;
反编译
ndisasm -o 0x7c00 boot.bin >> disboot.asm
00007C00 8CC8 mov ax,cs
00007C02 8ED8 mov ds,ax
00007C04 8EC0 mov es,ax
00007C06 E80200 call 0x7c0b
00007C09 EBFE jmp short 0x7c09
00007C0B B81E7C mov ax,0x7c1e
00007C0E 89C5 mov bp,ax
00007C10 B91000 mov cx,0x10
00007C13 B80113 mov ax,0x1301
00007C16 BB0C00 mov bx,0xc
00007C19 B200 mov dl,0x0
00007C1B CD10 int 0x10
00007C1D C3 ret
00007C1E 48 dec ax
00007C1F 656C gs insb
00007C21 6C insb
00007C22 6F outsw
00007C23 2C20 sub al,0x20
00007C25 4F dec di
00007C26 53 push bx
00007C27 20776F and [bx+0x6f],dh
00007C2A 726C jc 0x7c98
00007C2C 642100 and [fs:bx+si],ax
00007C2F 0000 add [bx+si],al
00007C31 0000 add [bx+si],al
00007C33 0000 add [bx+si],al
00007C35 0000 add [bx+si],al
00007C37 0000 add [bx+si],al
00007C39 0000 add [bx+si],al
00007C3B 0000 add [bx+si],al
00007C3D 0000 add [bx+si],al
00007C3F 0000 add [bx+si],al
00007C41 0000 add [bx+si],al
00007C43 0000 add [bx+si],al
00007C45 0000 add [bx+si],al
00007C47 0000 add [bx+si],al
00007C49 0000 add [bx+si],al
00007C4B 0000 add [bx+si],al
00007C4D 0000 add [bx+si],al
00007C4F 0000 add [bx+si],al
00007C51 0000 add [bx+si],al
00007C53 0000 add [bx+si],al
00007C55 0000 add [bx+si],al
00007C57 0000 add [bx+si],al
00007C59 0000 add [bx+si],al
00007C5B 0000 add [bx+si],al
00007C5D 0000 add [bx+si],al
00007C5F 0000 add [bx+si],al
00007C61 0000 add [bx+si],al
00007C63 0000 add [bx+si],al
00007C65 0000 add [bx+si],al
00007C67 0000 add [bx+si],al
00007C69 0000 add [bx+si],al
00007C6B 0000 add [bx+si],al
00007C6D 0000 add [bx+si],al
00007C6F 0000 add [bx+si],al
00007C71 0000 add [bx+si],al
00007C73 0000 add [bx+si],al
00007C75 0000 add [bx+si],al
00007C77 0000 add [bx+si],al
00007C79 0000 add [bx+si],al
00007C7B 0000 add [bx+si],al
00007C7D 0000 add [bx+si],al
00007C7F 0000 add [bx+si],al
00007C81 0000 add [bx+si],al
00007C83 0000 add [bx+si],al
00007C85 0000 add [bx+si],al
00007C87 0000 add [bx+si],al
00007C89 0000 add [bx+si],al
00007C8B 0000 add [bx+si],al
00007C8D 0000 add [bx+si],al
00007C8F 0000 add [bx+si],al
00007C91 0000 add [bx+si],al
00007C93 0000 add [bx+si],al
00007C95 0000 add [bx+si],al
00007C97 0000 add [bx+si],al
00007C99 0000 add [bx+si],al
00007C9B 0000 add [bx+si],al
00007C9D 0000 add [bx+si],al
00007C9F 0000 add [bx+si],al
00007CA1 0000 add [bx+si],al
00007CA3 0000 add [bx+si],al
00007CA5 0000 add [bx+si],al
00007CA7 0000 add [bx+si],al
00007CA9 0000 add [bx+si],al
00007CAB 0000 add [bx+si],al
00007CAD 0000 add [bx+si],al
00007CAF 0000 add [bx+si],al
00007CB1 0000 add [bx+si],al
00007CB3 0000 add [bx+si],al
00007CB5 0000 add [bx+si],al
00007CB7 0000 add [bx+si],al
00007CB9 0000 add [bx+si],al
00007CBB 0000 add [bx+si],al
00007CBD 0000 add [bx+si],al
00007CBF 0000 add [bx+si],al
00007CC1 0000 add [bx+si],al
00007CC3 0000 add [bx+si],al
00007CC5 0000 add [bx+si],al
00007CC7 0000 add [bx+si],al
00007CC9 0000 add [bx+si],al
00007CCB 0000 add [bx+si],al
00007CCD 0000 add [bx+si],al
00007CCF 0000 add [bx+si],al
00007CD1 0000 add [bx+si],al
00007CD3 0000 add [bx+si],al
00007CD5 0000 add [bx+si],al
00007CD7 0000 add [bx+si],al
00007CD9 0000 add [bx+si],al
00007CDB 0000 add [bx+si],al
00007CDD 0000 add [bx+si],al
00007CDF 0000 add [bx+si],al
00007CE1 0000 add [bx+si],al
00007CE3 0000 add [bx+si],al
00007CE5 0000 add [bx+si],al
00007CE7 0000 add [bx+si],al
00007CE9 0000 add [bx+si],al
00007CEB 0000 add [bx+si],al
00007CED 0000 add [bx+si],al
00007CEF 0000 add [bx+si],al
00007CF1 0000 add [bx+si],al
00007CF3 0000 add [bx+si],al
00007CF5 0000 add [bx+si],al
00007CF7 0000 add [bx+si],al
00007CF9 0000 add [bx+si],al
00007CFB 0000 add [bx+si],al
00007CFD 0000 add [bx+si],al
00007CFF 0000 add [bx+si],al
00007D01 0000 add [bx+si],al
00007D03 0000 add [bx+si],al
00007D05 0000 add [bx+si],al
00007D07 0000 add [bx+si],al
00007D09 0000 add [bx+si],al
00007D0B 0000 add [bx+si],al
00007D0D 0000 add [bx+si],al
00007D0F 0000 add [bx+si],al
00007D11 0000 add [bx+si],al
00007D13 0000 add [bx+si],al
00007D15 0000 add [bx+si],al
00007D17 0000 add [bx+si],al
00007D19 0000 add [bx+si],al
00007D1B 0000 add [bx+si],al
00007D1D 0000 add [bx+si],al
00007D1F 0000 add [bx+si],al
00007D21 0000 add [bx+si],al
00007D23 0000 add [bx+si],al
00007D25 0000 add [bx+si],al
00007D27 0000 add [bx+si],al
00007D29 0000 add [bx+si],al
00007D2B 0000 add [bx+si],al
00007D2D 0000 add [bx+si],al
00007D2F 0000 add [bx+si],al
00007D31 0000 add [bx+si],al
00007D33 0000 add [bx+si],al
00007D35 0000 add [bx+si],al
00007D37 0000 add [bx+si],al
00007D39 0000 add [bx+si],al
00007D3B 0000 add [bx+si],al
00007D3D 0000 add [bx+si],al
00007D3F 0000 add [bx+si],al
00007D41 0000 add [bx+si],al
00007D43 0000 add [bx+si],al
00007D45 0000 add [bx+si],al
00007D47 0000 add [bx+si],al
00007D49 0000 add [bx+si],al
00007D4B 0000 add [bx+si],al
00007D4D 0000 add [bx+si],al
00007D4F 0000 add [bx+si],al
00007D51 0000 add [bx+si],al
00007D53 0000 add [bx+si],al
00007D55 0000 add [bx+si],al
00007D57 0000 add [bx+si],al
00007D59 0000 add [bx+si],al
00007D5B 0000 add [bx+si],al
00007D5D 0000 add [bx+si],al
00007D5F 0000 add [bx+si],al
00007D61 0000 add [bx+si],al
00007D63 0000 add [bx+si],al
00007D65 0000 add [bx+si],al
00007D67 0000 add [bx+si],al
00007D69 0000 add [bx+si],al
00007D6B 0000 add [bx+si],al
00007D6D 0000 add [bx+si],al
00007D6F 0000 add [bx+si],al
00007D71 0000 add [bx+si],al
00007D73 0000 add [bx+si],al
00007D75 0000 add [bx+si],al
00007D77 0000 add [bx+si],al
00007D79 0000 add [bx+si],al
00007D7B 0000 add [bx+si],al
00007D7D 0000 add [bx+si],al
00007D7F 0000 add [bx+si],al
00007D81 0000 add [bx+si],al
00007D83 0000 add [bx+si],al
00007D85 0000 add [bx+si],al
00007D87 0000 add [bx+si],al
00007D89 0000 add [bx+si],al
00007D8B 0000 add [bx+si],al
00007D8D 0000 add [bx+si],al
00007D8F 0000 add [bx+si],al
00007D91 0000 add [bx+si],al
00007D93 0000 add [bx+si],al
00007D95 0000 add [bx+si],al
00007D97 0000 add [bx+si],al
00007D99 0000 add [bx+si],al
00007D9B 0000 add [bx+si],al
00007D9D 0000 add [bx+si],al
00007D9F 0000 add [bx+si],al
00007DA1 0000 add [bx+si],al
00007DA3 0000 add [bx+si],al
00007DA5 0000 add [bx+si],al
00007DA7 0000 add [bx+si],al
00007DA9 0000 add [bx+si],al
00007DAB 0000 add [bx+si],al
00007DAD 0000 add [bx+si],al
00007DAF 0000 add [bx+si],al
00007DB1 0000 add [bx+si],al
00007DB3 0000 add [bx+si],al
00007DB5 0000 add [bx+si],al
00007DB7 0000 add [bx+si],al
00007DB9 0000 add [bx+si],al
00007DBB 0000 add [bx+si],al
00007DBD 0000 add [bx+si],al
00007DBF 0000 add [bx+si],al
00007DC1 0000 add [bx+si],al
00007DC3 0000 add [bx+si],al
00007DC5 0000 add [bx+si],al
00007DC7 0000 add [bx+si],al
00007DC9 0000 add [bx+si],al
00007DCB 0000 add [bx+si],al
00007DCD 0000 add [bx+si],al
00007DCF 0000 add [bx+si],al
00007DD1 0000 add [bx+si],al
00007DD3 0000 add [bx+si],al
00007DD5 0000 add [bx+si],al
00007DD7 0000 add [bx+si],al
00007DD9 0000 add [bx+si],al
00007DDB 0000 add [bx+si],al
00007DDD 0000 add [bx+si],al
00007DDF 0000 add [bx+si],al
00007DE1 0000 add [bx+si],al
00007DE3 0000 add [bx+si],al
00007DE5 0000 add [bx+si],al
00007DE7 0000 add [bx+si],al
00007DE9 0000 add [bx+si],al
00007DEB 0000 add [bx+si],al
00007DED 0000 add [bx+si],al
00007DEF 0000 add [bx+si],al
00007DF1 0000 add [bx+si],al
00007DF3 0000 add [bx+si],al
00007DF5 0000 add [bx+si],al
00007DF7 0000 add [bx+si],al
00007DF9 0000 add [bx+si],al
00007DFB 0000 add [bx+si],al
00007DFD 0055AA add [di-0x56],dl
下面代码是一个综合,注意使用宏。ps,宏这个东西还是需要自己来控制的,别认为编译器给你自动处理了
;%define _BOOT_DEBUG_ ;做boot sector时一定将此行注释掉!
;将此行打开后使用 nasm boot.asm -o boot.com
;做成一个.com文件易于调试
%ifdef _BOOT_DEBUG_
org 0100h ;调试状态,做成.com文件,可调试
%else
org 07c00h ;告诉编译器程序加载到7c00处,boot 状态,bios将把boot sector 加载到0:7c00处执行
%endif
mov ax,cs
mov ds,ax
mov es,ax
call DispStr ;调用显示字符串例程
jmp $ ;无限循环
DispStr:
mov ax, BootMessage
mov bp, ax ;es:bp = 串地址
mov cx, 16 ;cx = 串长度
mov ax, 01301h ;ah=13,al=01h
mov bx, 000ch ;页号为0(bh=0) 黑底红字(bl=0ch,高亮)
mov dl, 0
int 10h ;10h号中断
ret
BootMessage: db "hello ,OS world!"
times 510-($-$$) db 0 ;填充剩下的空间,使生成的二进制代码恰好为
;512字节
dw 0xaa55 ;结束标记