[英]Why does my string-print function print extra garbage / file contents after the C string I pass it?
我决定制作一个 function 将 EAX 寄存器中的字符串打印到控制台,首先显示一条消息,然后再显示一些内容
代码(NASM):
newLine equ 0xa
section .data
msg db "Hello world!", newLine, 0
section .text
global _start
_start:
mov eax, msg
call printf
call exit
printf:
push eax
push ebx
push ecx
push edx
mov ecx, eax
call lenFind
mov edx, eax
mov eax, 4
mov ebx, 1
int 0x80
pop edx
pop ecx
pop ebx
pop eax
ret
lenFind:
push edx
mov edx, 0
.find:
cmp [eax+edx], byte 0
je .close
inc edx
jmp .find
.close:
pop edx
ret
exit:
mov eax, 1
mov ebx, 0
int 80h
Output:
Hello world!
.shstrtab.text.data
�J�
我知道它可能是什么,但最有可能的问题是我使用的是 32 位寄存器
您的lenFind不返回任何结果!
长度是在EDX
寄存器中计算的,但您已从堆栈中恢复了该长度。 然而,调用程序似乎期望长度在EAX
寄存器中:
lenFind:
push edx
mov edx, 0
.find:
cmp [eax+edx], byte 0
je .close
inc edx
jmp .find
.close:
MOV EAX, EDX <<< Add this line
pop edx
ret
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.