I try to use
movq $hello, %rdi #first parameter
xorl %eax, %eax #0 - number of used vector registers
movq $60, %rax
movq $0, %rdi
gcc -nostdlib try_printf.s -o try_printf -lc
XXX$ echo $?
XXX$ echo $output
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).
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.
That is what happened: As explained here, the C-library buffers the the standard streams as follows:
Which case applies is decided when
printf is called for the first time for a stream.
printf_try is called directly in the terminal, the output of the program can be seen because
\n at the end (which triggers the flush in the line-buffered mode) and it is a terminal, also the 2. case.
$(./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-