[英]Trying to sum numbers , but loop doesn't stop
在汇编中给出以下代码:
.section .rodata
input_format: .string "%d"
output_format: .string "%d\n"
.text
.globl main
.type main,@function
main:
pushl %ebp
movl %esp,%ebp
movl $0,%ebx # reset the sum register
movl $0,%esi # reset the counter of the numbers
movl $0,%eax # in order to know when to stop the loop
.loop:
addl $-8,%esp # moving down the stack
pushl %esp
pushl $input_format
call scanf # call scanf to get number from the user
addl $8,%esp
addl (%esp),%ebx # add the number to the total summary
movl (%esp),%ecx
addl $1,%esi # add 1 to the counter
pushl $output_format
call printf # print the given number
cmpl %ecx,%eax
jne .loop
# return from printf:
movl $0,%eax
movl %ebp,%esp
popl %ebp
ret
我试图对ebx
大于0的数字求和并最终计算它们的平均值,但是由于某种原因,当我放置“ 0”时循环不会停止(假设0是循环的结尾)。 我哪里做错了?
您的循环会在ecx和eax比较时终止。 eax设置为0,但是ecx似乎没有保持递减计数值。 另外,您确定函数正确保存了寄存器值吗?
嗯,我敢打赌,所有函数返回时都在修改eax-这是存储返回值的位置。 将您的eax分配0(或“ 0” / $ 30)下移到比较之前。 无论如何都不需要设置它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.