为什么我的双位数字在这个冒泡排序中会发生变化?

时间:2015-04-21 10:02:28

标签: assembly x86 bubble-sort

因此,在程序集x86中实现bubble sort的HW问题。它有效,但出于一些奇怪的原因,如果我有一个两位数(10+),它会修改一些值。

所以之前和之后是

  

5 7 1 4 3 9 2 10 8 5
  1 2 3 4 5 5 7 7 8 9

这是我的代码

main_loop:                      ; Begin the main loop
    mov cnt, 1                  ; Set the count to 1 (1-10)
    mov flag, 0                 ; And the flag (detects if anything changed)

    inner_loop:                 ; Loop through all values

        mov eax, cnt            ; Move the count to eax
        mov ebx, 4              ; Place 4 into ebx for multiplication
        mul ebx                 ; Multiply by 4 since we have ints
        mov ebx, aa[eax - 4]    ; Move (n - 1) to ebx
        cmp ebx, aa[eax]        ; Check if (n - 1) <= (n)
        jle end_loop            ; If it is, skip the next section


        mov ebx, aa[eax]        ; Move (n)     into ebx
        mov edx, aa[eax - 4]    ; Move (n - 1) into edx
        mov aa[eax], edx        ; Move (n - 1) into n
        mov aa[eax - 4], ebx    ; Move (n) into (n - 1)
        mov flag, 1             ; Set the changed flag

        end_loop:               ; End loop label

        inc cnt                 ; Increase the count
        cmp cnt, 10             ; Check if we've made it to 10 yet

        jle inner_loop          ; If not, repeat the inner loop

    cmp flag, 0                 ; Check if we changed anything
    je loop_end                 ; If we didn't, go to the end

    jmp main_loop               ; (Else) Jump to the beginning of the loop

感谢。

0 个答案:

没有答案