Assembly 尝试对数字求和,但循环不';停不下来

Assembly 尝试对数字求和,但循环不';停不下来,assembly,average,Assembly,Average,给定程序集中的以下代码: .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

给定程序集中的以下代码:

.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)下移到比较之前。在需要之前,无需进行设置。

关于函数是否正确保留寄存器值的问题,我不知道,因为我还没有找到一个能与汇编程序一起工作的调试器。关于eax和ecx,你认为ecx有问题吗?我只想将用户给定的值存储到ecx中,不需要倒计时。仅在输入“0”时停止循环。谢谢:)VisualStudio在汇编方面做得很好。所以你的scanf函数返回了一个字符码,用于输入什么?如果是这样,那么您得到的是ascii“0”,而不是0。尝试将eax值设置为“0”(我相信是30美元),并使用该值。这两个都将与“0”进行比较。问题是,我需要使用gcc编译器。Visual Studio是否有gcc编译器?是和否。是的,您可以在Visual Studio中编译gcc,但不可以使用其调试器。GDB似乎是gcc内容的首选调试器,尽管我在这里远不是专家。更改eax值可以解决您的问题吗?好的,我完成了。我尝试过操纵Eclipse来处理汇编,并且成功了。调试器工作得很好,问题是每次迭代(while循环)后,寄存器都不“记住”它们的值。我想我必须使用堆栈来保存值,并且永远不信任寄存器。谢谢