简体   繁体   中英

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.

#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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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