x86 汇编 - printf 在没有“\n"的情况下不打印 [英] x86 Assembly - printf doesn't print without "\n"

查看:63
本文介绍了x86 汇编 - printf 在没有“\n"的情况下不打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我很困惑.我正在阅读从头开始编程"这本书,并且正在使用库.

只要我在字符串中包含一个\n",printf 就可以正常工作,但如果没有它,它绝对不会打印任何内容.

知道为什么会这样吗?

代码:

<前>.section .data我的_str:.ascii "吉米乔 %d 岁了!\n\0"我的号码:.long 76.section .text.globl _start_开始:pushl my_numpushl $my_str调用 printfmovl $1, %eaxmovl $0, %ebx整数 $0x80

此外,当我将 -m elf_i386 用于 32 位模式并使用 -dynamic-linker/lib/ld-linux.so.2 -lc 进行链接时,我收到警告

<块引用>

ld: 在搜索 -lc 时跳过不兼容的/usr/lib64/libc.so

如果这有什么不同,或者有人对如何让它直接加载 32 位库有任何建议.

谢谢!

解决方案

问题在于 printf 默认情况下只是将内容打印到 stdout 缓冲区中.在刷新缓冲区之前,实际上不会打印内容.这取决于 stdout 的缓冲模式,但默认情况下,它是行缓冲的,这意味着每次打印换行符时它都会刷新.

要在 C 中显式刷新,您可以调用 fflush;你可以在 asm 代码中使用

pushl 标准输出调用刷新addl $4, %esp

或者,您可以调用 stdlib exit 函数(它在实际退出之前刷新所有 I/O 缓冲区),而不是使用 _exit 系统调用,后者不会.

So I'm confused. I'm going through the book "Programming from the Ground Up" and am working with using libraries.

printf is working just fine so long as I include a "\n" in the string, but without it it will print absolutely nothing.

Any idea why this happens?

Code:

.section .data

my_str:
        .ascii "Jimmy Joe is %d years old!\n\0"

my_num: 
        .long 76

.section .text

.globl _start
_start:
        pushl my_num
        pushl $my_str
        call printf

        movl $1, %eax
        movl $0, %ebx
        int $0x80

Also, when I use -m elf_i386 for 32-bit mode and -dynamic-linker /lib/ld-linux.so.2 -lc to link, I get the warning

ld: skipping incompatible /usr/lib64/libc.so when searching for -lc

If that makes any difference, or if anybody has any suggestions as to how to have it load the 32-bit library directly.

Thanks!

解决方案

The problem is that printf by default just prints stuff into the stdout buffer. Things won't actually be printed until the buffer is flushed. The depends on the buffering mode of stdout, but, by default, it is line-buffered, which means it gets flushed every time you print a newline character.

To flush explicitly in C, you call fflush; you can do that in asm code with

pushl stdout
call fflush
addl $4, %esp

Alternately, you can call the stdlib exit function (which flushes all I/O buffers before actually exiting), instead of using the _exit system call, which does not.

这篇关于x86 汇编 - printf 在没有“\n"的情况下不打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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