A new line written in a string constant is not recognized by nasm

I am writing a Hello World program using Assembler. I declared 2 string constants with a new line character \n at the end of each line:

 section .data str1: db "abcd\n" str2: db "efgh\n" section .text global _start _start: mov rax, 1 mov rdi, 1 mov rsi, str1 mov rdx, 6 syscall mov rax, 1 mov rdi, 1 mov rsi, str2 mov rdx, 6 syscall mov rax, 60 mov rdi, 0 syscall 

After I created and executed this code, I got the following result:

 $ nasm -f elf64 -o first.o first.asm $ ld -o first first.o $ ./first abcd\nefgh\n$ 

Why is the new line character \n printed?

+6
source share
2 answers

You need to use "backquotes" around the line to support escape sequences:

 str1: db `abcd\n` str2: db `efgh\n` 

Link: http://www.nasm.us/doc/nasmdoc3.html

3.4.2 Character strings:

"Strings enclosed in backquotes support C-style styles for special characters."

+7
source

Another approach is to put the 0xA ASCII code for the new line:

 section .data str1: db "abcd", 0xA str2: db "efgh", 0xA 
+5
source

Source: https://habr.com/ru/post/984078/


All Articles