繁体   English   中英

ASM printf:如果字符串不包含 \n 换行符,则无输出

[英]ASM printf: no output if string doesn't include \n newline

这段代码在屏幕上打印 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

但是,如果我从 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

程序不工作。 有什么建议么?

退出系统调用(相当于 C 中的_exit )不会刷新标准输出缓冲区。

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

如果你愿意在 libc 中调用printf ,你应该不会因为以同样的方式调用exit而感到难过。 在你的程序中有一个int $0x80并不会让你成为一个裸机坏蛋。

至少您需要push stdout;call fflush push $0;call fflush fflush(NULL)刷新所有输出流)

您需要清理传递给printf的参数,然后刷新输出缓冲区,因为您的字符串中没有新行:

.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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM