Question

This piece of code prints Hello on the screen

.data
    hello: .string "Hello\n"
    format: .string "%s" 
.text
    .global _start 
    _start:

    push $hello
    push $format
    call printf

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

But if I remove '\n' from hello string, like this:

.data
    hello: .string "Hello"
    format: .string "%s" 
.text
    .global _start 
    _start:

    push $hello
    push $format
    call printf

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

Program doesn't work. Any suggestions?

Was it helpful?

Solution

The exit syscall (equivalent to _exit in C) doesn't flush the stdout buffer.

Outputting a newline causes a flush on line-buffered streams, which stdout will be if it is pointed to a terminal.

If you're willing to call printf in libc, you shouldn't feel bad about calling exit the same way. Having an int $0x80 in your program doesn't make you a bare-metal badass.

At minimum you need to push stdout;call fflush before exiting. Or push $0;call fflush. (fflush(NULL) flushes all output streams)

OTHER TIPS

You need to clean up the arguments you passed to printf and then flush the output buffer since you don't have new line in your string:

.data
    hello: .string "Hello"
    format: .string "%s" 
.text
    .global _start 
    _start:

    push $hello
    push $format
    call printf
    addl $8, %esp
    pushl stdout
    call fflush
    addl $4, %esp
    movl $1, %eax   #exit
    movl $0, %ebx
    int $0x80
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top