简体   繁体   English

Null 字符未作为程序集 (NASM) 中的字符串结尾

[英]Null character is not acting as end of string in assembly (NASM)

In c language we can use '\0' null character as end of string.在 c 语言中,我们可以使用 '\0' null 字符作为字符串的结尾。

#include <stdio.h>
int main() {
    char msg[] = "hello\0world";
    printf("msg = %s", msg);
}

This code will print just hello .此代码将只打印hello Not world.不是世界。

But In linux x86 NASM I'm using the following code but it is printing helloworld .但是在 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

Why null character is not working here as end of string?为什么 null 字符在这里不能用作字符串结尾? And what can I do to get that in assembly?我该怎么做才能在装配中得到它?

You use the write system call to write the data with the exact size you specify.您使用write系统调用以您指定的确切大小写入数据。 The write function have no knowledge of the data it writes, it's just a series of bytes. write function 不知道它写入的数据,它只是一系列字节。

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.如果你想写一个以 null 结尾的字符串,你要么需要找到终止符的 position 并从那个 position 计算长度,要么使用一个知道 C 以 null 结尾的字符串的 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM