程序集:printf 不打印新行 [英] Assembly: printf not printing new line

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

问题描述

我有以下代码打印传递给 ./main 的参数数量.注意 rodata 部分中的 fmt.我已经包含了新行 ,就像在 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 , just like in C, but instead of printing the new line, it prints:

参数数量:1

我的代码是:

;main.asm
GLOBAL main
EXTERN printf

section .rodata:
fmt db "Number of parameters: %d 
", 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 上,您可以像这样将 编码为 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 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 
`, 0

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

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

3.4.2 字符串

一个字符串最多由八个字符组成,用单引号 ('...')、双引号 ("...") 或反引号 (...) 括起来.单引号或双引号等价于 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天全站免登陆