gpt4 book ai didi

程序集 - 读取虚拟磁盘的下一个扇区

转载 作者:行者123 更新时间:2023-12-04 18:47:57 29 4
gpt4 key购买 nike

作为世界上任何一名程序员,他/她一生中至少有一次,我正在尝试创建我的“革命性”,新的且唯一的操作系统。 :D

好吧,我正在使用虚拟模拟器(Oracle VM Virtual Box),为此我创建了一个带有 vmdk 磁盘的新 unknwon 操作系统。我喜欢 vmdk,因为它们只是普通文件,因此我可以将引导加载程序粘贴到虚拟硬盘的前 512 个字节上。

现在,我正在尝试读取这个虚拟磁盘的下一个扇区,我将在该扇区上粘贴一个简单的内核来显示一条消息。

我有两个问题:

  • 我是否正确读取了第二段(前 -512 字节 - 被引导加载程序占用)?
    代码:
    ReadDisk:
    mov bx, 0x8000 ; segment
    mov es, bx
    mov bx, 0x0000 ; offset

    mov ah, 0x02 ; read function
    mov al, 0x01 ; sectors - this might be wrong, trying to read from hd
    mov ch, 0x00 ; cylinder
    mov cl, 0x02 ; sector
    mov dh, 0x00 ; head
    mov dl, 0x80 ; drive - trying to read from hd
    int 0x13 ; disk int
    jc ReadDisk
    jmp [es:bx] ; buffer

    在这里,我在检查 CF 后收到错误消息。但是,如果我使用 INT 13, 1 获得最后
    状态消息,AL 为 0 - 所以不会保存错误。
  • 我是否将我的简单内核粘贴到 vmdk 内的正确位置?我所做的是将它粘贴到文件的第 512 个字节之后,前 512 个字节,正如我所说的,是引导加载程序。
    该文件将如下所示:
    BE 45 7C E8 16 00 EB FE B4 0E B7 00 B3 07 CD 10 <- First sector
    C3 AC 08 C0 74 05 E8 EF FF EB F6 C3 B4 00 B2 80
    CD 13 BE 5D 7C 72 F5 BB 00 80 8E C3 BB 00 00 B4
    02 B0 06 B5 00 B1 01 B6 00 B2 07 CD 13 BE 4E 7C
    72 CF 26 FF 27 57 65 6C 63 6F 6D 65 21 00 52 65
    61 64 69 6E 67 20 65 72 72 6F 72 21 00 52 65 73
    65 74 74 69 6E 67 20 65 72 72 6F 72 21 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 AA <- Boot-loader signature
    B4 0E B0 2E CD 10 EB FE 00 00 00 00 00 00 00 00 <- Start of the second sector
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

  • 所以,这就是我尝试将内核添加到第二个扇区的方式。
    你认为这有什么问题?谢谢!

    更新

    好的。我现在没有收到任何错误,但我没有看到正在执行的加载代码。它应该在窗口上显示一个点:
    ;--------------------------------------------
    ; 'load.asm'
    ; loaded from 'boot.asm'

    [org 0x8000]
    [bits 16]

    ;--------------------------------------------

    main:
    mov ah, 0x0E ; print function
    mov al, '.' ; ascii char
    int 0x10 ; IO int

    jmp $ ; hang

    最佳答案

    一个问题在这里:

    jmp [es:bx]

    这将从寄存器 es 中包含的地址处的内存位置读取一个地址,一个 16 位的偏移量。 (段部分)和 bx (偏移部分)然后设置 ip到那个 16 位偏移量。

    您可能想要使用的是:
    jmp some_constant1:some_constant2

    这将设置 cssome_constant1ipsome_constant2 .不出所料,这两个常量的合适候选分别是 0x8000 和 0,因为这是您的代码被加载的位置。

    现在,第二个问题在这里:
    [org 0x8000]

    org告诉 NASM 以这样一种方式生成代码,如果在偏移 0x8000 处加载它就可以工作。现在,偏移量 0x8000 与段 0x8000 不是一回事。如果您使用 jmp 0x8000:0 ,那么您还应该使用:
    [org 0]

    关于程序集 - 读取虚拟磁盘的下一个扇区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11074483/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com