我在汇编中遇到了一些问题,想要逐个访问字符串中的每个字符。我有以下代码调用print_string
例程,在将'Hello World!', 0
赋值给bx
寄存器之前:
mov bx, HELLO_MSG
call print_string
HELLO_MSG:
db 'Hello, World!', 0
在
print_string
函数中,我可以通过以下方法打印字符串的第一个字符:mov al, [bx]
; Trigger a single character print
mov ah, 0x0e
int 0x10
在我对汇编的基本理解中,第一个字符 (H
) 的地址被保存到了 bx
中,所以通过执行 mov al, [bx]
我对指针进行了解引用,并将真实值 H
赋给了 al
。
基于这个理解(如有错误请指出),我尝试了以下方法:
mov cl, bx ; Move the pointer to `cl`
add cl, 1 ; Do pointer arithmetic to add one byte to the address (hopefully referencing the next character)
mov al, [cl] ; Dereference the address
但是我得到了一个错误,指向mov al,[cl]
这一行:
error: invalid effective address
我也尝试了以下方法:
mov al, [bx] ; Move the dereferenced address to `al` (so `al` has `H`)
add al, 1 ; Increment `al`, but of course I'm getting the ASCII value of `H` + 1, which is not the next character in the string.