x86 CMP 指令不适用于单字比较

x86 CMP instruction not working for a single word comparison

我在比较单个字(2 个字节)时遇到了 CMP 指令的问题。
以下是我的main.asm:

[org 0x7c00]

mov bx, HELLO_MSG
call print_string

mov bx, GOODBYE_MSG
call print_string

jmp $

%include "print_string.asm"

; Data
HELLO_MSG:
    db 'Hello, World!', 0
GOODBYE_MSG:
    db 'Goodbye!', 0

这是print_string.asm文件:

print_string:
    pusha
    mov ah, 0x0e
    loop:
        call print_char
        cmp word [bx], 0
        jne loop
    popa
    ret

print_char:
    mov al, [bx]
    int 0x10
    add bx, 1
    ret

此代码打印出以下内容:

Hello World! Goodbye!Goodbye!

我知道当我添加

db 0

HELLO_MSGGOODBYE_MSG 之间,代码将按预期工作。谁能解释为什么 CMP 仅在字符串之间有 4 个字节的 0 时才有效?

如果有人有兴趣查看编译后的输出,请看这里:

bb  23  7c  e8  08  00  bb  31  7c  e8  02  00  eb  fe  60  b4
0e  e8  07  00  83  3f  00  75  f8  61  c3  8a  07  cd  10  83           
c3  01  c3  48  65  6c  6c  6f  2c  20  57  6f  72  6c  64  21
00  47  6f  6f  64  62  79  65  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  55  aa

最后的55 aa是我加的,因为我是用这段代码做boot loader

CMP 指令不能用于单字比较的原因是一个字必须包含 2 个字节。由于编译输出显示两条消息之间只有 1 个字节,因此您需要将 CMP 指令的大小从字更改为字节:

cmp byte [bx], 0

我错误地假设编译后的输出格式按 2 排列字节。但是,在 Michael Petch 的以下评论的帮助下:

Line 4 starts with 00 47 6f Each one of those values (separated by a space) is a single byte, not 2 bytes. 00 is a single byte of 0, not two bytes of 0

我意识到了我的错误。