我尝试使用printf
从我的汇编代码,这是一个小例子,这应该只是打印hello
到stdout:装配用printf会导致一个空的输出中
.section .rodata
hello:
.ascii "hello\n\0"
.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
:
XXX$ ./try_printf
hello
XXX$ echo $?
0
XXX$
但是,当我试图捕获文本,很明显,有些事情是不能正常工作:
XXX$ output=$(./try_printf)
XXX$ echo $output
XXX$
变量output
应有的价值hello
,但是是空的。
我的使用printf
有什么问题?
使用系统调用退出不会刷新_C_运行时使用的输出缓冲区。使用'call exit'替换exit syscall(退出也是_C_库的一部分) –
如果您查看[手册页](http://man7.org/linux/man-pages/man3/exit.3 .html)退出(退出(3)),你会发现这在描述中_所有打开的stdio(3)流被刷新并关闭。由tmpfile(3)创建的文件被删除._。当使用'movq $ 60,%rax movq $ 0,%rdi 系统调用' –