从iso映像启动,引导加载程序的内存地址为何不为0x7c00

问题描述 投票:1回答:1

我出于学习目的编写了一个小型引导程序,它将打印出引导程序第一条指令的内存地址,绝对是0x7c00。参见下面的汇编源代码,运行良好。

boot.s

.code16
.global init
init:
  mov $0x07c0, %ax
  mov %ax, %ds
  mov $0x07e0, %ax
  mov %ax, %ss
  mov $0x2000, %sp

  call next
next:
  pop %bx
  sub $(next-init), %bx  # starting point of memory address, now stored in %bx
  call print_register
  jmp .

print_register:  # always print out value in %bx
  mov %bh, %cl
  shr $0x4, %cl
  and $0x0f, %cl
  call print_digit
  mov %bh, %cl
  and $0x0f, %cl
  call print_digit
  mov %bl, %cl
  shr $0x4, %cl
  and $0x0f, %cl
  call print_digit
  mov %bl, %cl
  and $0x0f, %cl
  call print_digit
  ret

print_digit: # %cl has digit to be printed
  cmp $0x9, %cl
  jg print_digit_atof
print_digit_1to9:
  add $0x30, %cl
  jmp print_digit_out
print_digit_atof:
  add $0x57, %cl
print_digit_out:
  mov %cl, %al
  mov $0x0e, %ah
  int $0x10
  ret

.=510
.byte 0x55
.byte 0xaa
as -o boot.o boot.s
ld -o boot.bin --oformat binary -e init boot.o

在VMWare Player中,创建一个虚拟机,并将boot.bin设置为软盘的内容,然后打开电源。我可以看到7c00打印在屏幕上。

到目前为止一切顺利。

参考此答案Creating a bootable ISO image with custom bootloader,但是现在如果我通过以下命令将boot.bin作为引导加载程序放入iso映像中,则:

dd if=/dev/zero of=floppy.img bs=1024 count=1440
dd if=boot.bin of=floppy.img seek=0 count=1 conv=notrunc
mkdir iso
cp floppy.img iso/
genisoimage -quiet -V 'MYOS' -input-charset iso8859-1 -o myos.iso -b floppy.img \
    -hide floppy.img iso/

并使用myos.iso启动虚拟机,屏幕上显示0000

为什么不是7c00


更新阅读答案后,当我打印出%cs时,我可以看到:

1. boot from floppy disk, start address is 0x0000:7c00
2. boot from cd rom, start address is 0x07c0:0000
assembly x86 bootloader osdev iso-image
1个回答
4
投票

这是由于对El Torito CD-ROM引导规范的常见误解造成的,该规范指出,默认情况下应将仿真的引导扇区加载到“ 7C0的传统段”。并没有说应该使用非传统的起始地址07C0:0000代替传统的0000:7C00,但是BIOS编写者将其解释为一项要求。

虽然您可以假设引导程序已加载到线性地址00007C00,并且BIOS在引导程序的第一个字节处开始执行代码,但是您不能假定CS:IP的任何特定值。尽管大多数引导区都可以编写为不依赖于加载到CS中的段值,但是由于JMP和CALL指令是相对的,所以如果您这样做,则需要放置一段远JMP指令,以确保CS加载了预期的值细分值:

  jmp $0x07c0,$start
start:

注意,在上面的示例中,对段(0x07c0)的选择是基于您的引导扇区的“ org”为0的事实,因此,引导扇区的第一个字节的偏移量假定为0,而不是0x7c00。这意味着将以上代码添加到引导程序的开头,它将始终打印0000

© www.soinside.com 2019 - 2024. All rights reserved.