2
votes

So I'm confused. I'm going through the book "Programming from the Ground Up" and am working with using libraries.

printf is working just fine so long as I include a "\n" in the string, but without it it will print absolutely nothing.

Any idea why this happens?

Code:

.section .data

my_str:
        .ascii "Jimmy Joe is %d years old!\n\0"

my_num: 
        .long 76

.section .text

.globl _start
_start:
        pushl my_num
        pushl $my_str
        call printf

        movl $1, %eax
        movl $0, %ebx
        int $0x80

Also, when I use -m elf_i386 for 32-bit mode and -dynamic-linker /lib/ld-linux.so.2 -lc to link, I get the warning

ld: skipping incompatible /usr/lib64/libc.so when searching for -lc

If that makes any difference, or if anybody has any suggestions as to how to have it load the 32-bit library directly.

Thanks!

2
Where is your printf code? If it's in a library have you checked the API documentation?user1864610
@MikeW I'm honestly not sure how to check where it's being loaded from. I appended how I'm linking the library to the OP. I've tried searching to find any similar problems or how to fix the linking warning but am coming up short. I'm finding it hard to search for much Assembly help with all of the different versions.Bitani
In C, it's implementation-defined as to whether characters output to stdout appear if the program ends without a \n also being output. Since you are probably calling printf from a C library you may be getting the same behaviourM.M
Since you're calling the exit system call, the standard library has no way of knowing the process is about to be torn down, so if it hasn't yet flushed its buffers, it is never going to.Jonathon Reinhart

2 Answers

7
votes

The problem is that printf by default just prints stuff into the stdout buffer. Things won't actually be printed until the buffer is flushed. The depends on the buffering mode of stdout, but, by default, it is line-buffered, which means it gets flushed every time you print a newline character.

To flush explicitly in C, you call fflush; you can do that in asm code with

pushl stdout
call fflush
addl $4, %esp

Alternately, you can call the stdlib exit function (which flushes all I/O buffers before actually exiting), instead of using the _exit system call, which does not.

0
votes

It seems you try to link your 32-bit program against the (system default) 64Bit c library. Check if you have libs32 packages installed. To find out which libraries a program or other dynamically loads froum the LD_LIBRARY_PATH use ldd <name_of_your_binary> As to why the newline is required I can only speculate that it flushes the output buffer. See also Why does printf not flush after the call unless a newline is in the format string?