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_Assembly_Masm_Insertion Sort_Irvine32 - Fatal编程技术网

Sorting 汇编中的插入排序

Sorting 汇编中的插入排序,sorting,assembly,masm,insertion-sort,irvine32,Sorting,Assembly,Masm,Insertion Sort,Irvine32,因此,我正在基于以下高级代码编写一个插入排序(在汇编中): void insertionSort(int data[ ], int arraySize) { int insert; int moveItem; for(int next=1; next<arraySize; next++) { insert=data[next]; //store the value in the current ele

因此,我正在基于以下高级代码编写一个插入排序(在汇编中):

void insertionSort(int data[ ], int arraySize) {
        int insert;
        int moveItem;
        for(int next=1; next<arraySize; next++) {
                    insert=data[next];  //store the value in the current element
                    moveItem=next;    //initialize location to place element

                    while((moveItem>0)&&(data[moveItem-1]>insert)) {
                               //shift element one slot to the right
                                data[moveItem]=data[moveItem-1];
                                moveItem--;
                    }  //end while

                    data[moveItem]=insert;
        } //end for
} //end insertionSort

我没有详细检查您的代码,但我注意到,
InsertionSort
似乎同时将
edx
用于两个不同的目的:作为数组的指针,以及保存数组中的一个值。如果没有其他问题,这肯定会收支平衡

因此,在InsertionSort的开头,您说的是
mov-edx,OFFSET-myArray
——它是指向数组的指针。然后,几行之后,
mov-edx,[eax-4]
——哦,不,这是数组中的一个值。几行之后,
cmp-eax,[edx]
——哦,不,现在它又是数组的指针了


也许最后一条指令应该是
cmp-edx[eax]
或其他什么?因为
eax
在这里似乎是数组的指针。

我没有详细检查您的代码,但我注意到
InsertionSort
似乎同时使用
edx
用于两个不同的目的:作为数组的指针和保存数组中的一个值。如果没有其他问题,这肯定会收支平衡

因此,在InsertionSort的开头,您说的是
mov-edx,OFFSET-myArray
——它是指向数组的指针。然后,几行之后,
mov-edx,[eax-4]
——哦,不,这是数组中的一个值。几行之后,
cmp-eax,[edx]
——哦,不,现在它又是数组的指针了


也许最后一条指令应该是
cmp-edx[eax]
或其他什么?因为,
eax
似乎是指向数组的指针。

勇敢的灵魂会说“这里有171行汇编程序,出了什么问题?”:-)只要说“…勇敢的灵魂会说“这里有171行汇编程序,出了什么问题?”:-)只要说“…我已经解决了你提到的问题,但它并没有解决我的问题。我还尝试在cmp中使用[eax],但它似乎抛出了一个错误。另外,有两个寄存器以该数组的偏移量开始,但我需要该偏移量在以后再次检查我是否没有离开该数组的边界。现在看起来更像这样:Il6:cmp esp,esi JG endinner mov ebp,[eax-4]mov[eax],ebp sub eax,4CMP eax,edx JNG endinner mov esp,[eax-4]loop L6I认为仅根据代码提供的信息来调试代码是不可行的“看起来有点像这样”!您能更新问题中的代码以反映您所做的任何修复吗?(但听起来好像您在实际设置之前正在使用
esp
)另外,如果我正确理解了
esp
中的含义,我认为您在
L6
之后立即进行的比较是错误的。我已经解决了您提到的问题,但它并没有解决我的问题。我还尝试了使用[eax]在cmp中,它似乎抛出了一个错误。另外,有两个寄存器以该数组的偏移量开始,但我需要偏移量在以后再次检查我是否没有离开该数组的边界。现在看起来更像这样:I L6:cmp esp,esi JG endinner mov ebp,[eax-4]mov[eax],ebp sub eax,4 cmp eax,edx JNG ENDINER mov esp,[eax-4]loop L6I认为调试代码是不可行的,因为它“看起来有点像这样”!能否请您更新问题中的代码以反映您所做的任何修复?(但听起来好像您在实际设置之前就在使用
esp
)此外,如果我正确理解
esp
的含义,我认为您在
L6
之后立即进行的比较是错误的。
TITLE Insertion Sort (main.asm)
INCLUDE Irvine32.inc
.data

elems = 20

myArray  sdword   elems dup(0)
str1 byte "Press enter" ,0
str2 byte "The array now is ",0
next sdword 1
start sdword ?

.code
main PROC
    call Clrscr
    call CreateRandom
    call Display
    call InsertionSort
    call Display
    exit
main ENDP

CreateRandom PROC
;;;;Creates 20 random numbers and populates myArray;;;;;
    call Randomize
    mov ecx, 20
    mov edx, OFFSET myArray

L1:                     
    call Random32                   ;create random number
    mov [edx], eax                  ; move random number to the appropriate spot in the array
    add edx, 4                      ; increase the address of what it is pointing to
    loop L1
    mov edx, OFFSET str1            ; "press enter to continue"
    call WriteString
    call ReadInt
    call Crlf
    ret
CreateRandom ENDP

Display PROC
;;;; Displays current form of the array myArray;;;;;
    mov edx, OFFSET str2                ; "The array now is:"
    call WriteString                    ; write string
    call Crlf
    mov esi, OFFSET myArray             ; offset of the array 
    mov ecx, 20                         ; index of the loop
L2:  
    mov eax, [esi]                      ; move array at that point to eax
    call WriteDec                       ; print out the array as a decimal
    call Crlf                           ; next line
    add esi, 4                          ; next element in the array
    loop L2
    call Crlf
    ret
Display ENDP

InsertionSort PROC
mov ecx, 19
mov edx, OFFSET myArray
mov ebx, OFFSET myArray            ; eax=next
add ebx, 4                        ;moves up the array to second element comparable to next

outterloop:
    mov    esi, [ebx]                    ; esi=data[next]
    mov eax, ebx                ;movelterm=ebx
    L1:
        mov edx, [eax-4]            ;move the number that is greater into edx
        mov [eax], edx                ;move the number into that 2nd element of the
        sub eax, 4
        mov esi, [eax]
        cmp eax, [edx]
        JNG endinner                        ; if the address is not greater than the first address, skip to the end
        mov edx, [eax-4]
        cmp edx, esi                        ; if the address is greater, than it already sorted, skip to end
        JG endinner
        loop L1
    endinner:
        mov [eax], esi  ; move first to the second to finish the sort
        add ebx, 4    ;move to the next element of the array
    inc next   ;counting outside loop
        cmp ecx, next
        JNE outterloop ;return to top of for loop

ret
InsertionSort ENDP

END main