主引导记录中的引导加载程序:
[BITS 16] ; 16 bit mode
[ORG 0x7C00] ; Boot loader start address
Boot:
; Initial, dl contains drive number
; Set data segment to code segment
mov ax, cs
mov ds, ax
mov es, ax
; Set the stack segment to 0xA000
add ax, 0xA000
mov ss, ax
mov sp, 0x00
; Reset the drive, dl contains drive number
mov ah, 0x00
int 0x13
; Read from drive, dl contains drive number
; Set up output location to 0x7E00: 0x00
mov ax, 0x7E00
mov es, ax ; Load to 0x7E00 : 0x00
mov bx, 0x00
ReadDrive:
mov ah, 0x02
mov al, 0x01 ; Read 1 sector
mov ch, 0x00 ; Read on cylinder 0
mov cl, 0x02 ; Read sector 2
mov dh, 0x00 ; Head number 0
int 0x13
jnc Success
; Print error (character F)
mov al, 0x46
call PrintChar
jmp ReadDrive ; Retry
PrintChar: ; Prints a single character
pusha
mov ah, 0x09
mov bh, 0x00
mov bl, 0x0F
mov cx, 0x01
int 0x10
popa
ret
Success:
jmp 0x7E00:0x00 ; Jump to 2nd stage bootloader
TIMES 510 - ($ - $$) db 0
DW 0xAA55 ; Boot signature
第二阶段引导程序的代码:
[BITS 16]
[ORG 0x7E00]
Boot2:
; Prints the character S to the screen
mov al, 0x53
mov ah, 0x09
mov bh, 0x00
mov bl, 0x0F
mov cx, 0x01
int 0x10
jmp $ ; Loop forever
TIMES 512 - ($ - $$) db 0 ; Fill rest of block
这段代码是使用以下代码编译并写入磁盘的:
nasm boot.asm -o boot.bin -f bin
nasm boot2.asm -o boot2.bin -f bin
dd if=boot.bin of=/dev/sd3 bs=512
dd if=boot2.bin of=/dev/sd3 bs=512 seek=1
这段代码所写的设备是一款16GB的USB驱动器。我用来启动此代码的电脑支持从USB启动,并像其他硬盘一样引导它们。为什么这段代码似乎无法执行呢?
[ORG 0x0000]
,并且代码的前几行使用mov ax, cs
mov ds, ax
初始化了 _DS_,会发生什么? - Michael Petchjmp 0x7E00:0x00
。这是物理地址 (0x7e00<<4)+0 或 0x7E000。我怀疑这不是你想要的。也许你的意思是jmp 0x07E0:0x00
,它将是 (0x07e0<<4)+0=0x7e00。 - Michael Petchdl
有驱动器号码吗? - wallykjmp 0x07E0:0x00
),那么JMP将会更改CS:IP为CS=0x07E0,IP=0x0000。你想让ORG等于IP。因此ORG应该是0x0000。 - Michael Petch