Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 使用内联汇编代码的冒泡排序缺陷_Sorting_Pointers_Stack_Inline Assembly_Bubble Sort - Fatal编程技术网

Sorting 使用内联汇编代码的冒泡排序缺陷

Sorting 使用内联汇编代码的冒泡排序缺陷,sorting,pointers,stack,inline-assembly,bubble-sort,Sorting,Pointers,Stack,Inline Assembly,Bubble Sort,我正在为我的计算机系统课程制作汇编代码中的冒泡排序函数 我的函数如下所示 int sorter(int* list, int count, int opcode) { __asm { mov ebx, opcode ; opcode in ebx push 0 ; outer count mov ecx, 0 ; start ecx at 0 t

我正在为我的计算机系统课程制作汇编代码中的冒泡排序函数

我的函数如下所示

 int sorter(int* list, int count, int opcode) {
 __asm {
        mov ebx, opcode             ; opcode in ebx
        push 0                      ; outer count
        mov ecx, 0                  ; start ecx at 0 to start loop
        mov esi, list               ; set esi to list value/ starting address
        jmp iloop                   ; jump to inner loop
    oloop:                          //OUTER LOOP
        push ecx                    ;
        mov ecx, 0                  ; put the 2nd value in the list in the inner loop count
    iloop:                          // inner loop
        mov edx, dword ptr[esi + 4 * ecx]; move first value in edx
        mov eax, dword ptr[esi + 4 + 4 * ecx]; move next value in list to eax
        cmp ebx, 2                  ; compare opcode with 2 
        je dsnd                     ; if opcode is equal to 2 then we are using descending order
        cmp eax, edx                ; compare values at eax and edx
        jg no_swap                  ; if value is greater eax(2nd value) then dont swap /ascending order
    cont:                           //continue from descend
        push edx                    ; push contents of edx to stack
        pop dword ptr[esi + 4 + 4 * ecx]; pop the contents on stack to address of value in eax
        push eax                    ; push value in eax to stack
        pop dword ptr[esi + 4 * ecx]; pop value on stack to address of value previously in eax
    no_swap:                        //no  value swap
        inc ecx                     ; increment inner count
        cmp ecx, count              ; compare inner loop count to acutal length
        jne iloop                   ; if not equal jump back to inner loop
        pop ecx                     ; get outer count
        inc ecx                     ; to check for n-1 in outer loop
        cmp ecx, count              ; compare outer loop count to length
        jne oloop                   ; if not equal jump back to outer loop
        jmp done                    ;
    dsnd:
        cmp eax, edx                ; compare values at eax and edx
        jl no_swap                  ; if value is less then dont swap
        jmp cont                    ; continue with loop

    done:
    }
其中,操作码为1表示升序,或为2表示降序, list是指向整数列表的指针,count是列表中整数的数量

对于升序排序,我的程序工作正常,但对于降序排序,我遇到了如下测试运行所示的问题:

input 10 -20 5 12 30 -5 -22 55 52 0
Number of integer = 10
Ascend_or_Descend = 1
-22 -20 -5 0 5 10 12 30 52 55    # correct

input 48 -24 48 -24 10 100 -10 60 -256 10 -10 4096 -1024 60 10 -10
Number of integer = 16
Ascend_or_Descend = 1
-1024 -256 -24 -24 -10 -10 -10 10 10 10 48 48 60 60 100 4096 # correct

input 10 -20 5 12 30 -5 -22 55 52 0
Number of integer = 10
Ascend_or_Descend = 2
4283780 55 52 30 12 10 5 0 -5 -20 # incorrect

input 48 -24 48 -24 10 100 -10 60 -256 10 -10 4096 -1024 60 10 -10
Number of integer = 16
Ascend_or_Descend = 2
1500056 4096 100 60 60 48 48 10 10 10 -10 -10 -10 -24 -24 -256 # incorrect

它似乎取了最低的值并将其与地址交换。我不是汇编方面的专家。

为了清晰起见,大多数编辑都是代码阻塞。我修正了一些很难阅读的散文。你在猜测中的交换很好,但我认为这不是地址,而是列表[-1]中的垃圾值。我建议为任务编写更高级别的伪代码,比如if Decenting then:转到dsnd,或者将此元素和下一个元素交换,看看您的代码是否符合更高级别的意图。