冒泡排序不使用汇编语言中的本地数组

时间:2022-03-02 03:52:05

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.

我正在尝试使用程序集创建冒泡排序。我已经成功完成了几乎相同的代码,只是现在我传入一个LOCAL数组而不是.data部分中定义的数组。一切都在运行,但似乎没有转换。

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

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?

我哪里错了?

2 个解决方案

#1


; 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.

编辑:啊......比那更糟糕。我认为你也必须在BubbleSort中“取消引用”它。

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...

Edit2;哎呀,你已经在交换中使用了edx。使用esi或edi,然后......

#2


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.

调用BubbleSort后,你错过了一个ret。我不确定你在哪里设置BP用于堆栈帧索引,但是当进入第二次执行BubbleSort时,堆栈将不会对齐相同。

call BubbleSort
ret

Or exit the code execution.

或者退出代码执行。

#1


; 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.

编辑:啊......比那更糟糕。我认为你也必须在BubbleSort中“取消引用”它。

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...

Edit2;哎呀,你已经在交换中使用了edx。使用esi或edi,然后......

#2


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.

调用BubbleSort后,你错过了一个ret。我不确定你在哪里设置BP用于堆栈帧索引,但是当进入第二次执行BubbleSort时,堆栈将不会对齐相同。

call BubbleSort
ret

Or exit the code execution.

或者退出代码执行。