3

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

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

4

2 回答 2

6

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

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

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

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

于 2013-11-14T16:37:05.157 回答
3

您需要清理传递给的参数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
于 2013-11-14T17:04:34.800 回答