空字符并不充当汇编(nasm)中字符串的末端

发布于 2025-01-24 02:51:11 字数 554 浏览 4 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风启觞 2025-01-31 02:51:11

您使用 write> write 系统打电话以写入您指定的确切大小的数据。 写入函数不了解其写入的数据,它只是一系列字节。

如果您想编写一个零终止的字符串,则需要找到终结器的位置并从该位置计算长度,或者使用了解C null终止的字符串的函数。

You use the write system call to write the data with the exact size you specify. The write 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文