我有以下代码打印传递给 ./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/