简体   繁体   English

nasm 无法识别字符串常量中的新行

[英]The new line characted in the string constant isn't being recognized by nasm

I'm writing a 'Hello world' program using Assembler.我正在使用汇编程序编写一个“Hello world”程序。 I've declared 2 string constants with the new line character \\n at the end of each string:我在每个字符串的末尾用换行符\\n声明了 2 个字符串常量:

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?为什么打印出换行符\\n

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参考:http: //www.nasm.us/doc/nasmdoc3.html

3.4.2 Character Strings: 3.4.2 字符串:

"Strings enclosed in backquotes support C-style -escapes for special characters." “用反引号括起来的字符串支持特殊字符的 C 样式转义。”

Another approach is to put the ASCII code 0xA for a new line:另一种方法是将 ASCII 代码 0xA 放入新行:

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

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

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