在装配中使用 printf 会在管道时导致空输出,但在终端上工作 [英] Using printf in assembly leads to empty output when piping, but works on the terminal
问题描述
我尝试从我的汇编代码中使用 printf
,这是一个最小的例子,它应该只将 hello
打印到标准输出:
I try to use printf
from my assembler code, this is a minimal example which should just print hello
to stdout:
.section .rodata
hello:
.ascii "hello
"
.section .text
.globl _start
_start:
movq $hello, %rdi # first parameter
xorl %eax, %eax # 0 - number of used vector registers
call printf
#exit
movq $60, %rax
movq $0, %rdi
syscall
我用
gcc -nostdlib try_printf.s -o try_printf -lc
当我运行它时,它似乎工作:字符串hello
被打印出来并且退出状态是0
:
and when I run it, it seems to work: the string hello
is printed out and the exit status is 0
:
XXX$ ./try_printf
hello
XXX$ echo $?
0
XXX$
但是当我尝试捕获文本时,很明显,有些地方工作不正常:
But when I try to capture the text, it is obvious, that something is not working properly:
XXX$ output=$(./try_printf)
XXX$ echo $output
XXX$
变量 output
的值应该是 hello
,但为空.
The variable output
should have the value hello
, but is empty.
我使用 printf
有什么问题?
What is wrong with my usage of printf
?
推荐答案
在使用 printf 等 stdio 函数后,使用 call exit
而不是原始的 _exit
系统调用. 这会在进行 exit_group
系统调用之前刷新 stdio 缓冲区(write
系统调用).
Use call exit
instead of a raw _exit
syscall after using stdio functions like printf. This flushes stdio buffers (write
system call) before making an exit_group
system call).
(或者如果你的程序定义了一个main
而不是_start
,从main
返回相当于调用exit
>. 你不能从 _start
中 ret
.) 调用 fflush(NULL)
也应该有效.
(Or if your program defines a main
instead of _start
, returning from main
is equivalent to calling exit
. You can't ret
from _start
.) Calling fflush(NULL)
should also work.
正如迈克尔解释的那样,动态链接 C 库是可以的.这也是自下而上编程" 书(见第 8 章).
As Michael explained, it is OK to link the C-library dynamically. This is also how it is introduced in the "Programming bottom up" book (see chapter 8).
然而,为了结束程序而不是绕过它,从 C 库中调用 exit
很重要,这是我错误地调用 exit-syscall代码>.正如迈克尔所暗示的,退出做了很多清理就像冲洗溪流.
However it is important to call exit
from the C-library in order to end the program and not to bypass it, which was what I wrongly did by calling exit-syscall
. As hinted by Michael, exit does a lot of clean up like flushing streams.
这就是发生的事情:正如here,C 库按如下方式缓冲标准流:
That is what happened: As explained here, the C-library buffers the the standard streams as follows:
- 没有缓冲标准错误.
- 如果标准输出/输入是终端,则它是行缓冲的.
- 如果标准输出/输入不是终端,则它是完全缓冲的,因此在原始退出系统调用之前需要刷新.
在第一次为流调用 printf
时决定哪种情况适用.
Which case applies is decided when printf
is called for the first time for a stream.
所以如果在终端直接调用printf_try
,可以看到程序的输出,因为hello
末尾有
(在行缓冲模式下触发flush)并且是终端,也是2.情况.
So if printf_try
is called directly in the terminal, the output of the program can be seen because hello
has
at the end (which triggers the flush in the line-buffered mode) and it is a terminal, also the 2. case.
通过 $(./printf_try)
调用 printf_try
意味着 stdout 不再是终端(实际上我不知道它是临时文件还是临时文件)内存文件),因此 3. 情况有效 - 需要显式刷新,即调用 C-exit
.
Calling printf_try
via $(./printf_try)
means that the stdout is no longer a terminal (actually I don't know whether is is a temp file or a memory file) and thus the 3. case is in effect - there is need for an explicit flush i.e. call to C-exit
.
这篇关于在装配中使用 printf 会在管道时导致空输出,但在终端上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!