My program won't sort arrays that are larger than 130

前端 未结 1 1892
甜味超标
甜味超标 2021-01-26 00:53

So my program is supposed to take in user input (an integer between 10 and 200) and print out an array of random numbers and print out a sorted version of that array. However, t

1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-26 01:06

    If you look at your registers, you'll see that edx is 0fA4h, which is larger than it should be at the line it crashes on. ecx is a negative number. This is a clue that your loop is executing after it should have stopped.

    The problem is that the greater branch will fall thru to the lesser branch. This will decrement ecx again, causing it to go negative and your loop will just keep running until you get the access violation.

    The quick fix is to put an unconditional jmp after the loop instruction under the greater label.

    A better fix is to combine the tails of the loops into a simpler conditional:

        cmp     esi, [edi + (eax * 4)] ; compare array[j] and array[i]
        jle     lesser
        mov     eax, edx
    lesser:
        inc     edx
        loop    secondLoop
    

    0 讨论(0)
提交回复
热议问题