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

364 Views Asked by At

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?

1

There are 1 best solutions below

0
On

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.