문제

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?

도움이 되었습니까?

해결책

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)

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top