空字符并不充当汇编(nasm)中字符串的末端
在C语言中,我们可以将'\ 0'null字符用作字符串的结尾。
#include <stdio.h>
int main() {
char msg[] = "hello\0world";
printf("msg = %s", msg);
}
此代码将仅打印 Hello 。不是世界。
但是在Linux X86 NASM中,我正在使用以下代码,但它是打印 helloworld 。
section .data
msg db "hello", 0, "world"
section .text
global main
main:
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, 20
int 0x80
mov eax, 1
mov ebx, 0
int 0x80
ret
为什么在这里不用作为字符串的结尾来工作?我该怎么做才能在集会中得到这一点?
In c language we can use '\0' null character as end of string.
#include <stdio.h>
int main() {
char msg[] = "hello\0world";
printf("msg = %s", msg);
}
This code will print just hello. Not world.
But In linux x86 NASM I'm using the following code but it is printing helloworld.
section .data
msg db "hello", 0, "world"
section .text
global main
main:
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, 20
int 0x80
mov eax, 1
mov ebx, 0
int 0x80
ret
Why null character is not working here as end of string? And what can I do to get that in assembly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用
write> write
系统打电话以写入您指定的确切大小的数据。写入
函数不了解其写入的数据,它只是一系列字节。如果您想编写一个零终止的字符串,则需要找到终结器的位置并从该位置计算长度,或者使用了解C null终止的字符串的函数。
You use the
write
system call to write the data with the exact size you specify. Thewrite
function have no knowledge of the data it writes, it's just a series of bytes.If you want to write a null-terminated string, you either need to find the position of the terminator and calculate the length from that position, or use a function that knows about C null-terminated strings.