(No output even on the terminal when the output doesn't include a newline has the same cause.)
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\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
I build it with
gcc -nostdlib try_printf.s -o try_printf -lc
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$
The variable output
should have the value hello
, but is empty.
What is wrong with my usage of printf
?
call exit
(exit is part of the C library as well) – Mckenzieexit
(exit(3)) you'll find this in the description All open stdio(3) streams are flushed and closed. Files created by tmpfile(3) are removed. . This isn't guaranteed when usingmovq $60, %rax movq $0, %rdi syscall
– Mckenzie