汇编:printf不打印新行 [英] Assembly: printf not printing new line

查看:88
本文介绍了汇编:printf不打印新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following code that prints the number of parameters passed to ./main. Notice the fmt in the rodata section. I've included the new line \n, just like in C, but instead of printing the new line, it prints:

参数数量:1 \ n

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 将其链接以创建我的可执行文件.

I know that including a 10 before the 0 and after the "Number..." in fmt will print it, but I want printf to do it. I assemble the code with NASM and then link it via GCC to create my executable.

推荐答案

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

When you use quotes or double quotes around a string in NASM, it doesn't accept C style escape sequences. On Linux you can encode \n as ASCII 10 like this:

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

还有另一种选择. NASM 支持反引号(反引号),这将允许 NASM 将它们之间的字符作为 C 样式转义序列进行处理.这也应该工作:

There is an alternative. NASM supports backquotes (backticks) which will allow NASM to process the characters between them as C style escape sequences. This should work as well:

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

请注意:这些不是单引号,而是反引号.这在 NASM文档中进行了描述:

Please note: Those are not single quotes, but backticks. This is described in the NASM documentation:

3.4.2字符串

3.4.2 Character Strings

一个字符串由最多八个字符组成,这些字符包含在单引号('...'),双引号("...")或反引号(...)中.单引号或双引号等效于NASM(当然,用单引号引起来的常量可以使双引号出现在反引号中,反之亦然);这些内容按原样表示. 用引号引起来的字符串支持C样式的特殊字符转义符.

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.

这篇关于汇编:printf不打印新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆