Why doesn't my MASM32 x86 Assembly selection sort code sort properly?

38 Views Asked by At

I'm trying to get a sort selection program running in MASM32 x86 Assembly and it's not really working the way I want. I'm using the IRVINE32 library for some functions like Randomize and Random32. The output I get is a comma separated list of numbers which aren't sorted. I'm new to assembly and Visual Studio. My code is below:

;----------------SELECTION SORT PROC------------------
;Procedure to sort an array
;Receives: Array OFFSET in EAX, array size in EBX
;Returns: Sorted array using same offsets
 
 
Sort PROC
LOCAL arraySize:DWORD
LOCAL min:DWORD
MOV arraySize, EBX
 
PUSHAD
;ESI - Outer loop counter
;ECX - Inner loop counter
 
MOV ESI, 0
OUTER:                               ; for (int i = 0; i < size - 1; i++)
    DEC arraySize
    CMP ESI, arraySize
    INC arraySize
    JAE FINISHED
 
    MOV min, ESI
    INC ESI
    MOV ECX, ESI
    DEC ESI
    INNER:                           ; for (int j = i+1; j < size; j++)
        CMP ECX, arraySize
        JAE IFSTATEMENT2
 
        IFSTATEMENT1:                ; if(array[j] < array[min])
            MOV EBX, ECX
            SHL EBX, 2               ; becomes j*4
            MOV EDX, [EAX + EBX]     ; becomes array[j]
 
            MOV EBX, 0
            MOV EBX, min
            SHL EBX, 2
 
            CMP EDX, [EAX + EBX]
            JGE SKIP_UPDATE
 
            MOV min, ECX
            SKIP_UPDATE:
                INC ECX
                JMP INNER
 
    IFSTATEMENT2:                   ;if (min != i)
        CMP min, ESI
        JE ENDIF2
        PUSH ESI
        PUSH ECX
 
        MOV EDX, 0
        MOV EBX, 0
        MOV ESI, 0
        MOV ECX, 0
 
        SHL ESI, 2
 
        MOV EDX, [EAX + ESI]         ; int temp = array[i]
        MOV ECX, min
        SHL ECX, 2                   ; here ECX equals the index of min
        MOV EBX, [EAX + ECX]     
        MOV [EAX + ESI], EBX         ; array[i] = array[min]
        MOV [EAX + ECX], EDX         ; array[min] = temp;
 
        POP ECX
        POP ESI
 
    ENDIF2:
        INC ESI
        JMP OUTER
 
FINISHED:
    POPAD
    ret
    Sort ENDP
 
 
;----------------MAIN PROC------------------
;Procedure that generates 10 random 32 bit signed integer values and sorts them 
 
; Not including full code, just the call to the function because stack overflow doesn't like posting full code I think

    MOV EAX, OFFSET myData
    MOV EBX, 10d
    CALL Sort

I've rewrote this code a couple times to no avail, I have asked AI which is no help, I attempted to debug on visual studio but it's just foreign language to me, I'm really uncomfortable with the format. The main code does work fine (it does produce random 32 bit unsigned integers), unless the call to the procedure and setting up the parameters is wrong, then the only issue is in the sort portion. I dont THINK it's an issue with unsigned vs signed values, I checked the jumps I used were correct for signed and unsigned, the logic looks good to me, I'm not really sure where to go.

0

There are 0 best solutions below