16

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

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 had built and executed this code and I got the following result:

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

Why the new line character \n is printed out?

Igor
  • 477
  • 5
  • 13

2 Answers2

23

You need to use 'backquotes' around the string to support escape sequences:

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

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

3.4.2 Character Strings:

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

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
11

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

section .data
    str1: db "abcd", 0xA
    str2: db "efgh", 0xA
taliezin
  • 931
  • 1
  • 14
  • 20