程序集:printf 不打印新行
Assembly: printf not printing new line
我有以下代码打印传递给 ./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
这样做。我 assemble 使用 NASM 的代码,然后 link 通过 GCC 创建我的可执行文件。
汇编器不是 C。C 编译器将 \n 理解为 ASCII 10 的转义码。汇编器不理解并将其视为两个字符。按照您的描述添加 10。
当您在 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.
我有以下代码打印传递给 ./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
这样做。我 assemble 使用 NASM 的代码,然后 link 通过 GCC 创建我的可执行文件。
汇编器不是 C。C 编译器将 \n 理解为 ASCII 10 的转义码。汇编器不理解并将其视为两个字符。按照您的描述添加 10。
当您在 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.