这段代码在屏幕上打印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)
刷新所有输出流)
关于linux - ASM printf : no output if string doesn't include\n newline,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19980518/