ASM printf奇怪的行为

Dim*_*ima 3 x86 assembly asmx nasm

这段代码在屏幕上打印Hello

.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
Run Code Online (Sandbox Code Playgroud)

但是如果我从hello字符串中删除'\n',就像这样:

.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
Run Code Online (Sandbox Code Playgroud)

程序不起作用.有什么建议?

小智 6

exit syscall(相当于_exitC中)不会刷新stdout缓冲区.

输出换行会导致行缓冲流上的刷新,如果它指向终端,则stdout将是.

如果你愿意printf用libc 打电话,那么你应该exit以同样的方式打电话.int $0x80在你的程序中有一个不会让你成为一个裸机坏蛋.

至少你需要push stdout;call fflush在退出之前.或者push $0;call fflush.(fflush(NULL)刷新所有输出流)