【发布时间】:2013-09-12 13:49:16
【问题描述】:
我在这里找到了代码,在 Stack Overflow 上获取字符串的长度。代码如下:
HELLO_MSG: db 'Hello, world!', 0
mov ebx, HELLO_MSG
call print_string
print_string:
call str_len
mov ecx, eax ; puts the length of the called string into ecx register
cdq ; clear the direction flag; not sure if this is needed
mov ah, 0x0e ; scrolling teleype BIOS routine
printing:
mov al, byte bl
int 0x10
inc ebx
loop printing
ret
str_len:
mov eax,ebx
l00p:
cmp byte[eax], 0
jz lpend
inc eax
jmp l00p
lpend:
sub eax, ebx ; puts the length of the string into eax register
ret
问题是当函数调用str_len时,它只循环了3次,有时循环了2次。当它打印时,它不会打印实际的字符串,而是一些随机字母。有谁知道为什么它不起作用?我正在尝试在引导扇区中运行它,所以我真的没有任何调试工具。
【问题讨论】:
-
您的程序偏移设置是否正确?
-
我已声明代码从
[org 0x7c00]开始,这是我设置的唯一其他内容。我还没有设置基指针和堆栈指针,因为我并没有真正在这些打印字符串中使用它们。这会影响它吗? -
这是完整的源代码吗?怎么说执行将从第一条
mov指令开始,而不是HELLO_MSG字符串(即执行垃圾)?此外,用于清除方向标志的指令是cld,而不是cdq(cdq用于在除法之前将eax符号扩展为edx)。