简体   繁体   English

在QEMU的自定义引导程序中显示字符串时出现随机字符

[英]random character in displaying string inside custom bootloader in QEMU

I'm following a tutorial that introduce you in the magic world of bootloader. 我正在关注一个教程,向您介绍引导加载程序的神奇世界。

The easiest example, print a character, works. 最简单的例子是打印一个字符。 Displaying a string gives me some problem: it displays random characters. 显示字符串给我一个问题:它显示随机字符。 It should display 12 characters, starting at the location inside si register 它应该显示12个字符,从si寄存器内部的位置开始

Here's the Nasm code (build command: nasm.exe bootloader.asm -f bin -o bootloader.bin ) 这是Nasm代码(构建命令: nasm.exe bootloader.asm -f bin -o bootloader.bin

[bits 16]
[org 0]
start:
    mov al, 68
    mov ah, 0x0E
    mov bh, 0x00
    mov bl, 0x07

    mov si, helloWorld
    call printString

    jmp $

printString:
    mov dx, 0
._loop:
    mov al, [si]
    int 0x10
    inc si

    inc dx
    cmp dx, 12
    jl ._loop

    ret


helloWorld:
    db 'AAAAAAAAAA'

times 510 - ($ - $$) db 0
dw 0xAA55

Then I create the .img file with dd.exe if=bootloader.bin of=bootloader.img count=1 bs=512 然后我用dd.exe if=bootloader.bin of=bootloader.img count=1 bs=512创建.img文件dd.exe if=bootloader.bin of=bootloader.img count=1 bs=512

It boots correctly in QEMU ( qemu-system-i386.exe ) (well, it loads, because my bootloader still not boot) (maybe it's a problem of QEMU -difficoult-) 它可以在QEMU( qemu-system-i386.exe )中正确引导(好了,它可以加载,因为我的引导加载程序仍然无法引导)(也许是QEMU -difficoult-的问题)

Here's the screenshot 这是屏幕截图 TA-DAH!

What's the problem in my code? 我的代码有什么问题?

You should use [org 0x7c00] since that is where your bootloader will be loaded. 您应该使用[org 0x7c00]因为这是引导加载程序的加载位置。 I suggest you read more about boot sequence from this OSDev Wiki article . 我建议您从OSDev Wiki文章中阅读有关引导顺序的更多信息

Yes you need ORG 0x7C00, so the addressing starts at 7C00h. 是的,您需要ORG 0x7C00,所以寻址从7C00h开始。 You also need to set up the DS (data segment) register before accessing data on [SI] as there is no guarantee all BIOSES will have set it to 0. 您还需要在访问[SI]上的数据之前设置DS(数据段)寄存器,因为不能保证所有BIOSES都将其设置为0。

Just do: MOV DX,CS MOV DS,DX at your start: label 只需做:MOV DX,CS MOV DS,DX在您开始时:标签

After you have set up the DS you can access any data structures there. 设置完DS后,您可以在那里访问任何数据结构。

For reference here are the first instructions of a Microsoft boot sector (after the jmp): 作为参考,这里是Microsoft引导扇区的第一条说明(在jmp之后):

XOR CX,CX
MOV SS,CX
MOV SP,7BF4h
MOV ES,CX
MOV DS,CX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM