问题描述
我正在开发一个简单的裸机操作系统,我的打印字符串功能仅适用于某些字符串(例如Hello World")而不适用于其他字符串(例如按 F1 寻求帮助")
I am developing a simple bare-metal OS, and my function for printing strings works only on some strings (eg "Hello World") but not others (eg "Press F1 for help")
[ORG 0x7C00]
msg db "Press F1 for help",0
main:
mov AH, 00h
int 16h
cmp AH, 0x3B
je help
jmp main
help:
mov si, msg
call print
jmp main
; Print library, invoke with "call print" example:
; msg db "Foobar",0
; mov SI, msg
; call print
%include "printlib.inc"
return:
ret
times 510-($-$$) db 0;
db 0x55
db 0xAA
printlib.inc:
printlib.inc:
print:
mov ax, 0x07c0
mov ds, ax
cld
jmp .loop
.loop:lodsb
or al, al ; zero=end or str
jz .retn ; get out
mov ah, 0x0E
mov bh, 0
int 0x10
jmp .loop
.retn:
ret
推荐答案
BIOS 将始终从引导扇区的第一个字节开始执行,在您的情况下,它似乎是字符串,因此您正在执行数据.(事实上,您放入了一个名为 main
的标签不会影响这一点;没有任何东西可以查看它.)这可能是因为您的Hello world"没有了.字符串恰好对应于不会完全破坏所有内容的指令.
The BIOS will always start execution at the first byte of the boot sector, and in your case that appears to be the string, so you're executing data. (The fact that you put in a label called main
doesn't affect this; nothing looks at it.) It could be that your "Hello world" string just happens to correspond to instructions that don't totally break everything.
尝试将字符串移动到所有代码之后,或者在它之前插入一个 jmp main
.
Try moving the string to be after all the code, or else insert a jmp main
before it.
此外,您的 ORG
指令和 ds
段之间存在不一致.您的引导扇区在线性地址 0x7c00
处加载.您可以在 segment:offset 形式中将其视为 0000:7c00
或 07c0:0000
(或其他方式,如果您真的想要的话).因此,要访问引导扇区中的数据,您需要将 ds
加载为零并使用 [ORG 0x7c00]
,或者使用 ds
加载0x07c0
并使用 [ORG 0]
.但是,您的代码混合了两者.
Also, you have an inconsistency between your ORG
directive and your ds
segment. Your boot sector gets loaded at linear address 0x7c00
. You can think of this in segment:offset form as 0000:7c00
or 07c0:0000
(or other ways in between if you really want). So to access data in the boot sector, you either need to load ds
with zero and use [ORG 0x7c00]
, or else load ds
with 0x07c0
and use [ORG 0]
. However, your code mixes the two.
这篇关于我的汇编函数打印一些字符串,但不打印其他字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!