I have a GAS build program for Linux x86-32 ending with the following:
movl $1, %eax movl $0, %ebx
When I exit like this, the program works fine, but if I try to read the output of stdout, I get nothing (using less or wc).
I tried compiling a minimal C program and comparing strace outputs. The only difference I found was that GCC made the C program ( int main() { printf("donkey\n"); } ) an implicit exit with exit_group(0) in the output of strace.
I tried changing my ASM program to exit with call exit instead of raw syscall. The standard version is now read as usual.
Test case
.data douout: .string "monkey\n" .text .globl main main: pushl $douout call printf
Compile and run:
$ yasm -g dwarf2 -f elf -p gas t.asm && gcc -g -melf_i386 -ot to && ./t | wc -c 0
Expected:
7
EDIT:
I tried calling both tcflush and fflush and I still have a problem. With fflush I even get segfault.
0xb7e9e7c9 in _IO_fflush (fp=0x804a018) at iofflush.c:42 42 iofflush.c: No such file or directory. in iofflush.c (gdb) bt #0 0xb7e9e7c9 in _IO_fflush (fp=0x804a018) at iofflush.c:42 #1 0x08048434 in main () at t.asm:12 (gdb) frame 1 #1 0x08048434 in main () at t.asm:12 12 call fflush (gdb) list 7 8 pushl $douout 9 call printf 10 # Exit 11 movl $0, %eax 12 call fflush 13 movl $1, %eax 14 movl $0, %ebx 15 int $0x80
EDIT2:
OK, now everything works. I used the wrong calling convention that I copied here: Printf without a newline in the assembly
The argument for fflush should be on the stack as usual.
$ cat t.asm .data douout: .string "monkey\n" .text .globl main main: pushl $douout call printf # Exit pushl $0 call fflush movl $1, %eax movl $0, %ebx int $0x80 $ yasm -g dwarf2 -f elf -p gas t.asm && gcc -g -melf_i386 -ot to && ./t | wc -c 7 $
Thanks to everyone, especially nos.