这是我的工作代码:
.section .data
prompt:
.asciz "Please input value:\n"
input:
.asciz "%d"
output:
.asciz "output: %d\n"
integer:
.int
.section .text
.globl main
main:
nop
pushl $prompt
call printf
addl $8, %esp
pushl $integer
pushl $input
call scanf
addl $8, %esp
movl integer, %ecx
pushl %ecx
pushl $output
call printf
add $8, %esp
pushl $0
call exit
为什么我改变了以下的顺序:
input:
.asciz "%d"
output:
.asciz "output: %d\n"
integer:
.int
到(integer
高于其input
)
integer:
.int
input:
.asciz "%d"
output:
.asciz "output: %d\n"
...然后它不再打印出您扫描的整数?是因为我们首先引用$ integer,当我们将它推入堆栈时?
答案 0 :(得分:1)
您需要为.int
实际指定一个值,否则它将不会执行任何操作。所以在第二种情况下,你的整数(4个字节)也与input
和output
的开头重叠。假设您输入的数字在最高有效字节中为零(即任何小于2^24
的非负数),您将有效地将输出字符串截断为零长度。
要解决此问题,您只需执行.int 0
或在.bss
部分中声明您的整数,例如使用.lcomm integer, 4
。