x86(masm32)中的冒泡排序,我写的那种排序不起作用

时间:2013-07-23 06:30:39

标签: x86 masm32

我正在尝试在x86(masm32)中编写冒泡排序。排序不起作用。我已经测试了一些代码,它似乎在比较和交换部分搞砸了。 由于某种原因,比较函数总是为EAX分配2。如果我能弄清楚为什么我可以使程序运作。

提前感谢您的帮助。

    .data
          aa DWORD 10 DUP(5, 7, 6, 1, 4, 3, 9, 2, 10, 8)
        count DWORD 0
; DB 8-bits, DW 16-bit, DWORD 32, WORD 16 BYTE 8



    .code                       ; Tell MASM where the code starts

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

start:                          ; The CODE entry point to the program

    mov esi, count ;loop count

    outer:
        inc esi
        mov edi, count        
        cmp esi, 9       
        je end_loop

    inner: ;this loop represents a full pass on the entire array
        inc edi        
        cmp edi, 9 ;after 9 passes goes to outer loop
        je outer

    compare:
        mov eax, [aa + edi * 4h] ;higher indexed one
        mov ebx, [aa + edi * 4h - 4h] 

;testing        print chr$(13,10)
;testing        print str$(eax)
;testing        print chr$(13, 10)
;testing        print str$(ebx)
;testing        print chr$(13, 10)

        cmp ebx, eax
        jg swap

    swap:
        mov [aa + edi * 4h], eax
        mov [aa + edi * 4h + 4], ebx
        jmp inner

end_loop:

    ;print out array elements
    sub esi, esi
    mov esi, [aa]
    print str$(esi)
    print chr$(" ")
    sub esi, esi

    mov esi, [aa + 4h]
    print str$(esi)
    print chr$(" ")
    sub esi, esi

    mov esi, [aa + 4h * 2]
    print str$(esi)
    print chr$(" ")
    sub esi, esi

    mov esi, [aa + 4h * 3]
    print str$(esi)
    print chr$(" ")
    sub esi, esi

    mov esi, [aa + 4h * 4]
    print str$(esi)
    print chr$(" ")
    sub esi, esi

    mov esi, [aa + 4h * 5]
    print str$(esi)
    print chr$(" ")
    sub esi, esi

    mov esi, [aa + 4h * 6]
    print str$(esi)
    print chr$(" ")
    sub esi, esi

    mov esi, [aa + 4h * 7]
    print str$(esi)
    print chr$(" ")
    sub esi, esi

    mov esi, [aa + 4h * 8]
    print str$(esi)
    print chr$(" ")
    sub esi, esi

    mov esi, [aa + 4h * 9]
    print str$(esi)
    print chr$(" ")
    sub esi, esi

exit


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start                       ; Tell MASM where the program ends

2 个答案:

答案 0 :(得分:1)

弄清楚出了什么问题 - 程序中间的打印声明正在记忆中。这是工作类型。感谢大家的帮助!

    .data
          aa DWORD 10 DUP(5, 7, 6, 1, 4, 3, 9, 2, 10, 8)
        count DWORD -1
; DB 8-bits, DW 16-bit, DWORD 32, WORD 16 BYTE 8



    .code                       ; Tell MASM where the code starts

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

start:                          ; The CODE entry point to the program

    mov esi, count ;loop count

    outer:
        inc esi
        mov edi, count        
        cmp esi, 10       
        je end_loop

    inner: ;this loop represents a full pass on the entire array
        inc edi        
        cmp edi, 9 ;after 9 passes goes to outer loop
        je outer

    compare:
        mov eax, [aa + edi * 4h]
        mov ebx, [aa + edi * 4h + 4] ;want to make this one the higher indexed-one

        ;print chr$(13,10) These print calls were hosing the memory before.
        ;print str$(eax)
        ;print chr$(13, 10)
        ;print str$(ebx)
        ;print chr$(13, 10)

        cmp eax, ebx
        jle inner

    swap:
        mov [aa + edi * 4h], ebx
        mov [aa + edi * 4h + 4], eax
        jmp inner

end_loop:

    ;print out array elements
    sub esi, esi
    mov esi, [aa]
    print str$(esi)
    print chr$(" ")
    sub esi, esi

    mov esi, [aa + 4h]
    print str$(esi)
    print chr$(" ")
    sub esi, esi

    mov esi, [aa + 4h * 2]
    print str$(esi)
    print chr$(" ")
    sub esi, esi

    mov esi, [aa + 4h * 3]
    print str$(esi)
    print chr$(" ")
    sub esi, esi

    mov esi, [aa + 4h * 4]
    print str$(esi)
    print chr$(" ")
    sub esi, esi

    mov esi, [aa + 4h * 5]
    print str$(esi)
    print chr$(" ")
    sub esi, esi

    mov esi, [aa + 4h * 6]
    print str$(esi)
    print chr$(" ")
    sub esi, esi

    mov esi, [aa + 4h * 7]
    print str$(esi)
    print chr$(" ")
    sub esi, esi

    mov esi, [aa + 4h * 8]
    print str$(esi)
    print chr$(" ")
    sub esi, esi

    mov esi, [aa + 4h * 9]
    print str$(esi)
    print chr$(" ")
    sub esi, esi

exit


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start                       ; Tell MASM where the program ends

答案 1 :(得分:0)

一个明显的问题就在这里:

    cmp ebx, eax
    jg swap

swap:
    mov [aa + edi * 4h], eax
    mov [aa + edi * 4h + 4], ebx
    jmp inner

这样,如果设置了g标志,它会跳转到交换 - 如果g标志设置,它会掉到交换状态,所以执行两种方式完全相同的代码。猜测,如果两个项目已经按顺序排列,你想要的东西可能就像jle inner一样跳过交换。

编辑:再看一遍,看起来你有另一个相当明显的问题。让我们考虑像C这样的东西,并使用base作为加载eaxcompare)的地址。你(概念上)做:

eax = *base;
ebx = *(base + 4);

然后在swap你做:

*base = eax;
*(base - 4) = ebx;

这显然也是错误的。要交换这两个变量,您需要以下内容:

*base = ebx;
*(base + 4) = eax;