Linux 字符串常量中的新行字符为';nasm不承认这一点
我正在用汇编程序编写一个“Hello world”程序。我已经声明了两个字符串常量,每个字符串末尾都有新行字符Linux 字符串常量中的新行字符为';nasm不承认这一点,linux,assembly,nasm,Linux,Assembly,Nasm,我正在用汇编程序编写一个“Hello world”程序。我已经声明了两个字符串常量,每个字符串末尾都有新行字符\n: 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
\n
:
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
在我构建并执行此代码之后,我得到了以下结果:
$ nasm -f elf64 -o first.o first.asm
$ ld -o first first.o
$ ./first
abcd\nefgh\n$
为什么要打印新行字符
\n
?您需要在字符串周围使用“反引号”来支持转义序列:
str1: db `abcd\n`
str2: db `efgh\n`
参考:
3.4.2字符串:
用反引号括起来的字符串支持C样式的特殊转义
人物。”
另一种方法是将ASCII码0xA用于新行:
section .data
str1: db "abcd", 0xA
str2: db "efgh", 0xA