我真的不知道你用什么样的汇编,但是我能得到你的代码用gcc编译,所以我坚持你的格式化风格(不是在谈论AT & T语法)。
无论如何,你应该检查documentation为scanf
,并意识到它需要一个格式字符串和指针在哪里存储读取的值存储位置,也返回数量的成功读取物品而不是读到的东西。
现在,请执行相同操作,并检查documention的printf
。你会看到需要一个格式字符串来以可读形式打印你的号码。一个合适的格式字符串是"%d\n"
来打印数字和一个换行符。
现在您的代码可能是这个样子(这编译并为我工作得很好用gcc):
.section .rodata
input_format: .string "%d"
output_format: .string "%d\n"
.section .bss
input: .long
.section .text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
pushl $input # push the ADDRESS of input to have the value stored in it
pushl $input_format # give scanf the ADDRESS of the format string
call scanf # call scanf to get number from the user
addl $8, %esp # clean up the stack
# Note the return value of scanf is passed through eax (same for printf)
pushl input # pass the number to printf BY VALUE
pushl $output_format # pass the ADDRESSS of the output format string to printf
call printf # print input
#return from printf:
movl $0, %eax
movl %ebp,%esp
popl %ebp
ret
注意,我通常会使用db/dw/dd
的,而不是在.(ro)data
和.bss
部分分配内存.string
和.long
,所以如果那部分做得有点不对,你可以修复它。
您也可以使用堆栈空间来存储数字,但是您已经声明了input
,并且我想让代码尽可能类似于您的代码。在scanf
和printf
之前和之后的所有其他东西都一样,我只是将它作为代码。
编辑:下面是一个使用堆栈来创建一个局部变量,而不是让在.bss
或.data
段声明的变量的例子:
.section .rodata
input_format: .string "%d"
output_format: .string "%d\n"
.section .text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $4, %esp # allocate 4 bytes on the stack for a local variable
# The local variable will be at -4(%ebp)
leal -4(%ebp), %eax # get the ADDRESS of our local variable
pushl %eax # push the ADDRESS of the variable on the stack
pushl $input_format # give scanf the ADDRESS of the format string
call scanf # call scanf to get number from the user
addl $8, %esp # clean up the stack
# Note the return value of scanf is passed through eax (same for printf)
pushl -4(%ebp) # pass the number to printf BY VALUE
pushl $output_format # pass the ADDRESSS of the output format string to printf
call printf # print the input
#return from printf:
movl $0, %eax
movl %ebp,%esp
popl %ebp
ret
首先的 - 谢谢!第二,我怎样才能做到这一点,而不使用全局变量(即'输入'变量)并使用栈?再次感谢! – ron 2011-12-16 06:29:02