我正在使用 Assembler 编写“Hello world”程序。我在每个字符串的末尾用换行符 \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
在我构建并执行这段代码后,我得到了以下结果:
$ 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`
引用:http://www.nasm.us/doc/nasmdoc3.html
3.4.2 字符串:
"Strings enclosed in backquotes support C-style -escapes for special characters."
关于linux - nasm 无法识别字符串常量中的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29181085/