Bootloader

只以x86 cpu 为例
开机后,x86 cpu会以Real Mode 执行FFFF:0000地址的指令,这个地址就是BIOS的代码的开始位置,之后BIOS就开始做一些初始化工作,例如内存检测、初始化中断控制和系统定时器。
之后BIOS会检查可以启动的设备,例如 CD、DVD、USB等等(甚至可以通过网络来)

这类可启动的东东,都有一个Bootsector,这个Bootsector是一个512字节大小的空间,512字节最后的两个字节如果标记了0xAA55
( 01010101   1010 1010        二进制看起来的左右对称1和0交替的 呵呵 注意x86是 小端法,高字节放高位,低字节放低位,所以前面的二进制倒转过来了)
    第511字节   第512字节

BIOS里面通常还有一个Boot优先级的控制(重装过系统的应该知道吧 选项就是disk 、 dvd等) 

BIOS选好一个能启动的设备后,会将Bootsector加载到地址为0000:7C00的位置,之后BIOS指令跳转到这个地址,CPU就开始执行Bootloader了。

从维基百科上面找到的一个例子。org 7C00说明这个东东的加载地址应该为7C00位置
Here is a simple bootloader demo designed for NASM:
nasm -f bin -o floppy.img floppy.asm
         org 7C00h
 
         jmp short Start ;Jump over the data (the 'short' keyword makes the jmp instruction smaller)
 
 Msg:    db "Hello World! "
 EndMsg:
 
 Start:  mov bx, 000Fh   ;Page 0, colour attribute 15 (white) for the int 10 calls below
         mov cx, 1       ;We will want to write 1 character
         xor dx, dx      ;Start at top left corner
         mov ds, dx      ;Ensure ds = 0 (to let us load the message)
         cld             ;Ensure direction flag is cleared (for LODSB)
 
 Print:  mov si, Msg     ;Loads the address of the first byte of the message, 7C02h in this case
 
                         ;PC BIOS Interrupt 10 Subfunction 2 - Set cursor position
                         ;AH = 2
 Char:   mov ah, 2       ;BH = page, DH = row, DL = column
         int 10h
         lodsb           ;Load a byte of the message into AL.
                         ;Remember that DS is 0 and SI holds the
                         ;offset of one of the bytes of the message.
 
                         ;PC BIOS Interrupt 10 Subfunction 9 - Write character and colour
                         ;AH = 9
         mov ah, 9       ;BH = page, AL = character, BL = attribute, CX = character count
         int 10h
 
         inc dl          ;Advance cursor
 
         cmp dl, 80      ;Wrap around edge of screen if necessary
         jne Skip
         xor dl, dl
         inc dh
 
         cmp dh, 25      ;Wrap around bottom of screen if necessary
         jne Skip
         xor dh, dh
 
 Skip:   cmp si, EndMsg  ;If we're not at end of message,
         jne Char        ;continue loading characters
         jmp Print       ;otherwise restart from the beginning of the message
 
 
 times 0200h - 2 - ($ - $$)  db 0    ;Zerofill up to 510 bytes
 
         dw 0AA55h       ;Boot Sector signature
 
 ;OPTIONAL:
 ;To zerofill up to the size of a standard 1.44MB, 3.5" floppy disk
 ;times 1474560 - ($ - $$) db 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值