user14351632
user14351632

Reputation:

Sorting through an array after filling in x86 MASM assembly language

can anyone help me with this problem, I'm new to assembly language and somewhat stuck on what to do next. Here's the code:

.data
evenStart    dword          11h
oddStart     dword          20h
darray       dword          15 dup (?)
.code
main PROC
    mov esi, OFFSET darray
    xor ecx, ecx
    
L1: 
    mov ebx, OFFSET evenStart
    test cl,1
    jz iseven
    mov ebx, OFFSET oddStart
    
iseven:
    mov eax, [ebx]
    inc dword ptr [ebx]
    mov dword ptr [esi + 4*ecx],eax
    inc ecx
    cmp ecx,15
    jb L1
    
    exit
main ENDP
END main

So the project requires me to fill the uninitilized array, which I did. But then it also ask me to sort through this array in descending order and then put the middle elements of the array into the eax register and call DumpRegs. This is the part where I got stuck in. Any help on how to proceed would be great. Thank you!

Upvotes: 0

Views: 1493

Answers (1)

Sep Roland
Sep Roland

Reputation: 39166

Next Bubble Sort uses nested loops. Because your array has 15 elements, the outer loop can do 14 comparisons during its 1st iteration. With each iteration of the outer loop it has to do 1 comparison less because the smallest element has bubbled towards the end of the array.

    mov ebx, 15-1      ; Outer loop iteration count
OuterLoop:
    mov esi, offset darray

    mov ecx, ebx       ; Inner loop iteration count
InnerLoop:
    lodsd
    mov edx, [esi]
    cmp eax, edx
    jge Skip
    mov [esi-4], edx   ; Swap these 2 elements
    mov [esi], eax
Skip:
    dec ecx
    jnz InnerLoop

    dec ebx
    jnz OuterLoop

The unsorted array: 11h, 20h, 12h, 21h, 13h, 22h, 14h, 23h, 15h, 24h, 16h, 25h, 17h, 26h, 18h

The sorted array: 26h, 25h, 24h, 23h, 22h, 21h, 20h, 18h, 17h, 16h, 15h, 14h, 13h, 12h, 11h

In this array with an odd number of elements (15), the element indexes range from 0 to 14. There is a true middle element at index 7.

Upvotes: 1

Related Questions