David
David

Reputation: 4873

Bubble sort not working with local array in assembly language

I'm trying to create a bubble sort with assembly. I've done so successfully with nearly identical code, only now I'm passing in a LOCAL array instead of one defined in the .data section. Everything runs, but there seems to be no switching.

Here is the code

start

call main

exit


main PROC
    LOCAL numArray[5]:DWORD    ; Create a local array for the numbers

    mov [numArray + 0], 5
    mov [numArray + 4], 4
    mov [numArray + 8], 3
    mov [numArray + 12], 2
    mov [numArray + 16], 1

    push numArray
    call BubbleSort

    ret

main ENDP

array EQU [ebp + 8]
FLAG EQU DWORD PTR [ebp - 4]
BubbleSort PROC
    enter 4, 0                      ; Enter with one int local memory (for flag)


    outer_bubble_loop:
        mov ecx, 1                  ; Set the count to 1 
        mov FLAG, 0                 ; And clear the flag (Detects if anything changes

        inner_bubble_loop:                  ; Loop through all values
            mov ebx, [array + ecx * 4 - 4]  ; Move the (n - 1)th index to ebx
            cmp ebx, [array + ecx * 4]      ; Compare ebx against the (n)th index
            jle end_loop                    ; If the result was less than or equal, skip the swapping part


            mov ebx, [array + ecx * 4]      ; Move (n)     into ebx
            mov edx, [array + ecx * 4 - 4]  ; Move (n - 1) into edx
            mov [array + ecx * 4], edx      ; Move (n - 1) into n
            mov [array + ecx * 4 - 4], ebx  ; Move (n)     into (n - 1)
            mov FLAG, 1                     ; Set the changed flag

            end_loop:                       ; End loop label

            inc ecx                         ; Increase the count
            cmp ecx, NDATES                 ; Check if we've made it to the end yet

            jl inner_bubble_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 outer_bubble_loop       ; (Else) Jump to the beginning of the loop


        loop_end:                   ; Loop end label

    leave
    ret
BubbleSort ENDP

My output is, strangely:

4
5
5
2
1

If I use a different data set, it doesn't do the duplication, but things still aren't moved.

Where am I going wrong with this?

Upvotes: 0

Views: 567

Answers (2)

Frank Kotler
Frank Kotler

Reputation: 3119

; push numArray
lea eax, numArray
push eax
call BubbleSort
...

... unless I'm mistaken...

Edit: Ahhh... worse than that. I think you're going to have to "dereference" it in BubbleSort, too.

mov edx, array ; [ebp + 8], right?
; then use edx instead of "array"... or so...

Edit2 ; Whoops, you're already using edx in the swap. Use esi or edi, then...

Upvotes: 1

Weather Vane
Weather Vane

Reputation: 34585

You are missing a ret after the call to BubbleSort. I am unsure where you are setting BP for the stack frame indexing, but when falling through to the second execution of BubbleSort the stack won't be aligned the same.

call BubbleSort
ret

Or exit the code execution.

Upvotes: 0

Related Questions