美文网首页
引导程序之起始地址

引导程序之起始地址

作者: louyang | 来源:发表于2020-06-06 00:01 被阅读0次

    实模式指的是所有内存地址都是实的,不是虚的。
    所有的x86处理器都支持实模式,而且出于兼容性考虑,所有的x86处理器上电后都进入16bit实模式。

    据说BIOS会把启动磁盘第一扇区的512字节复制到内存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
      call print_register
      jmp .
    
    print_register:
      mov %bh, %dl
      shr $0x4, %dl
      and $0x0f, %dl
      call print_digit
      mov %bh, %dl
      and $0x0f, %dl
      call print_digit
      mov %bl, %dl
      shr $0x4, %dl
      and $0x0f, %dl
      call print_digit
      mov %bl, %dl
      and $0x0f, %dl
      call print_digit
      ret
    
    print_digit: # %dl has digit to be printed
      cmp $0x9, %dl
      jg print_digit_atof
    print_digit_1to9:
      add $0x30, %dl
      jmp print_digit_out
    print_digit_atof:
      add $0x57, %dl
    print_digit_out:
      mov %dl, %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 
    
    image.png
    参考

    https://www.jianshu.com/writer#/notebooks/19149135/notes/71268850

    相关文章

      网友评论

          本文标题:引导程序之起始地址

          本文链接:https://www.haomeiwen.com/subject/cevzzhtx.html