【发布时间】:2013-09-23 14:45:43
【问题描述】:
我对汇编还很陌生,并试图从标准输入中读取一个值(从 C 中调用 scanf 函数)并将其打印回标准输出(使用 printf)。
.text
readstr: .asciz "%d" #string used for calling scanf
printstr: .asciz "%d\n" #string used for calling printf
.global main
main: movl %esp, %ebp #initialize base pointer
call inout
pushl %eax
pushl $printstr
call printf
pushl $0 #push exit code to stack
call exit #exit with the exit code
inout: pushl %ebp
movl %esp, %ebp
subl $4, %esp
leal -4(%ebp), %eax
pushl %eax
pushl $readstr
call scanf
movl %ebp, %esp
popl %ebp
ret
预期的输出将与输入的数字相同,但输出始终是
1
注意:在 64 位 suse linux 企业桌面上编译,使用 gcc -m32 -o inout inout.s
这里出了什么问题?
【问题讨论】:
-
您正在编写非常不正确的汇编代码,没有正确获取函数参数和返回值,并使堆栈不平衡。使用 C 编译器并查看其汇编输出以确保正确。
-
我强烈建议您学习如何使用调试器。然后,您可以单步执行代码,检查寄存器和内存位置以查看它们是否包含您希望它们在每个点包含的内容。如果您的期望和您看到的不匹配,要么您的代码被破坏,要么您的理解被破坏,您可以调试其中任何一个。