冒泡排序在86(MASM32),我写的那种不工作冒泡排序在86(MASM32),我写的那种不工作(B

2019-05-12 12:18发布

我试着写在86(MASM32)冒泡排序。 排序不起作用。 我测试过的一些代码,它出现在比较和交换的部分被搞砸了。 出于某种原因,比较函数总是指派2至EAX。 如果我能找出原因,我可以得到程序的工作。

感谢您的帮助提前。

    .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

Answer 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


Answer 2:

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

    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从whcih您加载地址eax (以compare )。 你(概念上)这样做:

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

然后,在swap你这样做:

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

这显然是错误的为好。 交换两个变量,你想要的东西,如:

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


文章来源: Bubble sort in x86 (masm32), the sort I wrote doesn't work
标签: x86 masm32