Bubble sort in x86 (masm32), the sort I wrote doesn’t work

Figured out what was wrong – the print statements in the middle of the program were hosing my memory. Here is the working sort. Thanks for the help everyone!

    .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

Leave a Comment