Jeb*_*ess 3 x86 assembly qemu bootloader x86-16
I am having troubles with the CMP instruction when comparing single words (2 bytes).
The following is my 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
Run Code Online (Sandbox Code Playgroud)
This is the print_string.asm file:
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
Run Code Online (Sandbox Code Playgroud)
This code prints out the following:
Hello World! Goodbye!Goodbye!
I know that when I add
db 0
Run Code Online (Sandbox Code Playgroud)
in between the HELLO_MSG and the GOODBYE_MSG, the code will work as intended. Can anyone explain why CMP will only work when there are 4 bytes of 0 between the strings?
If anyone is interested in looking at the compiled output, here it is:
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
Run Code Online (Sandbox Code Playgroud)
The 55 aa at the end is added by me because I am using this code as a boot loader.
CMP 指令不适用于单字比较的原因是一个字必须包含 2 个字节。由于编译的输出显示两条消息之间只有 1 个字节,因此您需要将 CMP 指令的大小从字更改为字节:
cmp byte [bx], 0
Run Code Online (Sandbox Code Playgroud)
我错误地假设编译后的输出格式按 2 排列字节。但是,在 Michael Petch 的以下评论的帮助下:
第 4 行以 00 47 6f 开头,每个值(用空格分隔)都是一个字节,而不是 2 个字节。00是单个字节的0,而不是两个字节的0
我能够认识到自己的错误。