【问题标题】:Custom bootloader does not access correct data during boot自定义引导加载程序在引导期间无法访问正确的数据
【发布时间】:2013-09-16 21:26:21
【问题描述】:

请注意,我是引导加载程序和程序集的菜鸟,所以我一直在关注一些(可能不是最好的)示例。 我正在为一个项目在 NASM 中编写引导加载程序。它只是在屏幕上打印一个字符串并等待 Enter 键。输入后它加载另一个扇区并打印第二个字符串。在 VBox 中进行测试非常棒:字符串已打印,一切正常。

在真实计算机上对其进行测试后,我得到的不是第一个字符串,而是垃圾。第二个程序运行良好。 我正在使用自定义脚本写入软盘。全部在 Linux Mint 下完成。 如有错误请见谅。

org 7c00h

start: 
    jmp short begin ; jump over the DOS boot record data
    ;----------------------------------------------------------------------
    ; data portion of the "DOS BOOT RECORD"
    ; ----------------------------------------------------------------------
    ;DOS data goes here
    ;------------------------------------------------------------------------
    mesaj db "Some string here" ,10
    len equ $-mesaj

begin:

    mov ah, 0
    mov al, 03h
    int 10h

    mov ah, 13h
    mov al, 0    
    mov cx, len
    mov bh, 0h
    mov bl, 02h      
    mov bp, mesaj
    mov dl, 20
    mov dh, 12
    int 10h ;printing the string

read:
    xor ah, ah
    int 16h ; read a key from keyboard
    cmp al, 0 
    jz read ; if special key, continue reading


    mov ah, 0Ah
    mov cx, 100
    mov bh, 0
    int 10h ; write keyboard character to screen
    cmp al, 0dh ; if character is Enter, load second program
    jne read

    ; loading the second sector
    mov ax, 0x500 
    mov es, ax    
    mov al, 1
    mov ch, 0
    mov cl, 2
    mov dh, 0
    mov dl, 0
    xor bx, bx

    mov ah, 2    
    int 0x13

    jmp 0x500:0

【问题讨论】:

  • “打印字符串”服务要求字符串指针位于es:bp。我看到你在哪里设置bp。你在哪里设置es
  • 我没有设置。如前所述,我对组装很陌生。由于在 VirtualBox 中一切正常,如果我阅读的示例在打印前设置了 es,我认为我不需要设置它...
  • 通话文档显示es:bp。有可能在 VirtualBox 中,es 恰好设置为您需要的(例如,可能设置为等于ds)。不过,你不能这样假设。最好遵守文档。

标签: assembly nasm bootloader


【解决方案1】:

感谢 mbratch 的评论,我已经成功了。因为没有设置es,所以es:bp 不指向我的字符串,而是指向内存中的某个随机位置。

答案是将es 设置为0,因为我使用的是绝对地址(即7c00h)。这仅在开头有 org 7c00h 时才有效。如果您不想/不能使用org,请将es 设置为7c0h

所以,要么:

org 7c00h
mov ax, 0
mov es, ax

或:

mov ax, 7c0h
mov es, ax 

我从http://wiki.osdev.org/Babystep2 获得信息,从 mbratch 获得想法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-23
    • 2016-10-25
    • 2011-10-02
    • 1970-01-01
    • 2014-10-03
    • 2017-06-14
    • 2016-03-20
    • 1970-01-01
    相关资源
    最近更新 更多