1

I have a dword array of 15 random integers that is stored in esi. I have a bubble sort algorithm below which is supposed to arrange the values from lowest to highest. However when my program runs through this algorithm it doe not seem to touch any of the values and when I print the array it is exactly as it was before. Thoughts?

BubbleSort PROC USES eax ebx ecx edx esi

    mov ecx, 0 
    mov ecx, 15

    OUTER_LOOP: 
        push ecx
        mov ecx,0
        mov ecx,14
        mov esi, OFFSET arr

        COMPARE:
            mov ebx,0
            mov edx,0
            mov bl, [esi]
            mov dl, [esi+1]
            cmp bl,dl
            jg SWAP 

            CONTINUE:      
                add esi,4      
                loop COMPARE

        mov esi, OFFSET arr

        pop ecx     
        loop OUTER_LOOP

    jmp FINISHED

    SWAP:
        mov bl, [esi]
        mov dl, [esi+1]
        xchg bl,dl 
        mov [esi],dl 
        mov [esi+1],bl
        jmp CONTINUE 

    FINISHED:
    ret

BubbleSort ENDP

Thanks for your help in advance.

1 Answers1

0

EDIT:

I found different problems in your code. I've fixed them below and ESI is actually changed (not sorted because I think the sorting algorithm is wrong but didn't have time yet to change it)

1- mov reg,0

It's better to xor the register instead of moving 0 to it. XOR instruction is shorter.

2- In the CONTINUE loop I use add esi,2 (maybe you're on 64bit windows?)

3- In the SWAP loop, the first 2 mov instructions are useless. I removed them.

4- In the SWAP loop, you need to swap the lines where you put the values back in esi and esi+1 (actually you don't even need the xchg instruction.)

Now ESI is changed. You need to work on the sorting algorithm.

BubbleSort PROC uses ECX
    xor ecx,ecx 
    mov ecx, 15

    OUTER_LOOP: 
        push ecx
        xor ecx,ecx
        mov ecx,14
        mov esi, OFFSET arr

        COMPARE:
            xor ebx,ebx
            xor edx,edx
            mov bl, byte ptr ds:[esi]
            mov dl, byte ptr ds:[esi+1]
            cmp bl,dl
            jg SWAP 

            CONTINUE:      
                add esi,2      
                loop COMPARE

        mov esi, OFFSET arr

        pop ecx     
        loop OUTER_LOOP

    jmp FINISHED

    SWAP:
        xchg bl,dl 
        mov byte ptr ds:[esi+1],dl 
        mov byte ptr ds:[esi],bl
        jmp CONTINUE 

    FINISHED:
    ret
BubbleSort ENDP

EDIT: In the outer loop you have 2 mov esi, OFFSET arr

and the second one happens just before exiting. So you're basically putting the array offset back in ESI like it was at the start.

The USES ESI note below holds. You need to remove it.

Also what is ECX used for in this procedure? It's never used but you're assigning values, pushig and poping them?


USES eax ebx ecx edx esi

This masm helper pushes ESI on the stack.

When you exit the procedure it pops it out as it was before.

So you probably don't want to include ESI up there.

Note, you also want to put your DWORD in another register like EAX. There are some case when entering a procedure ESI and the likes are pushed anyway.

龚元程
  • 417
  • 1
  • 5
  • 14
  • so putting my array in esi and then entering a procedure could be pushing and popping the esi registry? –  Oct 13 '11 at 04:39
  • hmm even though I get rid of `USES esi` and even trying the eax registry I get the same output as before. –  Oct 13 '11 at 04:41
  • the value being pushed in to ecx is for looping. I am pushing and popping it so it loops the correct number of times. Also I have removed the second `mov esi,OFFSET arr` and I get the same result. –  Oct 13 '11 at 05:02
  • You don't need an `xchg` instruction: just store the registers into opposite places from where you loaded them. Also, instead of xor-zero and then `mov` loads of bytes, just use `movzx eax, byte ptr [esi]`. [It's faster than 2 instructions](https://stackoverflow.com/questions/13092829/any-way-to-move-2-bytes-in-32-bit-x86-using-mov-without-causing-a-mode-switch-or/47516645#47516645) And if you care about efficiency at all, [don't use the `loop` instruction](https://stackoverflow.com/questions/35742570/why-is-the-loop-instruction-slow-couldnt-intel-have-implemented-it-efficiently). – Peter Cordes Nov 28 '17 at 07:47