linux - nasm 无法识别字符串常量中的换行符

标签 linux assembly nasm

我正在使用 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/

相关文章:

linux - Jenkins 上带有单声道的 MSBUILD

c - 保护 x86-64 上的内存

performance - 组装一些与 nasm 宏不相符的代码

linux - Cygwin:从 32 位文件到 64 位文件

linux - 有没有办法检索用户定义命令调用的脚本路径?

linux - 使用 Bash 脚本启动新终端

python - 关于实现pyinotify实例监控目录的问题

delphi - 使用 ASM 调用对象方法

assembly - 字节ptr和字ptr的区别

assembly - 使用 esp 寄存器