ASM printf奇怪的行为

时间:2013-11-14 14:40:18

标签: assembly x86 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

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

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

2 个答案:

答案 0 :(得分:6)

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

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

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

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

答案 1 :(得分: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