【发布时间】:2017-10-22 06:20:19
【问题描述】:
我使用 nasm 一次打印一个字符串,但它一次打印两个字符串,我在末尾添加了空字符,我正在比较空字符以检查字符串的结尾,但两个字符串即使我只要求其中一个,最终也会被打印出来。
[org 0x7c00]
mov bx, HELLO_MSG
call print_string
mov bx, GOODBYE_MSG
jmp $
%include "print_string.asm"
;Data
HELLO_MSG:
db 'Hello, World!',0
GOODBYE_MSG:
db 'GOODBYE!',0
times 510-($- $$) db 0
dw 0xaa55
另一个文件 print_string.asm
print_string:
pusha
cld
mov ah,0x0e
config: mov al,[bx]
;Comparing the strings
mov cx,[bx]
cmp cx,0x00 ;Comparing for null
jne print
je end
print: int 0x10
add bx,1
jmp config
end: popa
ret
【问题讨论】:
-
mov cx,[bx]该指令读取 bx 处的 WORD(即 2 个字节)。由于没有 WORD 为 0,因此此循环永远不会结束。 -
只是一个观察。
pusha/popa是 80186 指令。如果编写可能针对多种硬件的引导加载程序,则应该只考虑 8086 指令。当然,如果目的是引导受保护模式的内核,那么这不是问题。您还应该显式设置 DS 段寄存器并使用 CLD 确保字符串指令向前移动。这些是对现有答案的观察。 -
@DavidWohlferd 感谢您的洞察力。我将该代码更改为。
config: mov al,[bx];Comparing the stringscmp byte [bx],0x00 ;Comparing for nulljne printje end它正在工作