linux - 程序集:printf 不打印新行

标签 linux assembly x86 printf nasm

我有以下代码打印传递给 ./main 的参数数量。注意 rodata 部分中的 fmt。我包含了新行 \n,就像在 C 中一样,但它不打印新行,而是打印:

Number of parameters: 1 \n

我的代码是:

;main.asm
GLOBAL main
EXTERN printf

section .rodata:
fmt db "Number of parameters: %d \n", 0 

section .text:

main:

    push ebp
    mov ebp, esp    ;stackframe

    push dword[ebp+8]       ;prepara los parametros para printf
    push fmt
    call printf
    add esp, 2*4

    mov eax, 0      ;return value

    leave           ;desarmado del stack frame
    ret

我知道在 fmt 中的 0 之前和“Number...”之后包括一个 10 将打印它,但我希望 printf 这样做。我用 NASM 组装代码,然后通过 GCC 链接它以创建我的可执行文件。

最佳答案

当您在 NASM 中的字符串周围使用引号或双引号时,它不接受 C 风格的转义序列。在 Linux 上,您可以像这样将 \n 编码为 ASCII 10:

fmt db "Number of parameters: %d", 10, 0 

还有一个选择。 NASM 支持反引号(反引号),这将允许 NASM 将它们之间的字符处理为 C 风格的转义序列。这也应该有效:

fmt db `Number of parameters: %d \n`, 0

请注意:这些不是单引号,而是反引号。这在 NASM documentation 中有描述。 :

3.4.2 Character Strings

A character string consists of up to eight characters enclosed in either single quotes ('...'), double quotes ("...") or backquotes (...). Single or double quotes are equivalent to NASM (except of course that surrounding the constant with single quotes allows double quotes to appear within it and vice versa); the contents of those are represented verbatim. Strings enclosed in backquotes support C-style -escapes for special characters.

关于linux - 程序集:printf 不打印新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36707877/

相关文章:

assembly - 从 CD 加载扇区

c++ - project00.exe : 0xC0000005: Access violation 中 0x93b3237d 的未处理异常

assembly - 为什么我的根目录没有被加载? (FAT12)

c - 如何使用共享对象库来启用/禁用功能?

java - 当我从一台 PC 有双头或三头时,如何知道分辨率屏幕宽度和高度?

assembly - 转换十六进制/十进制数 (Assembly-TASM)

sorting - 代码优化技巧 :

gcc - 使用外部C代码编译ASM引导加载程序

linux - 删除以 # 开头的字符串以及 # 之后的所有内容

c - 在 Linux 上使用 fgets() 读取带有 DOS 行结尾的文件