1

だから私はこの高レベルのコードに基づいて(アセンブリで)挿入ソートをコーディングしています:

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

myArrayと呼ばれる配列には正確に20個の乱数があります。私たちの本に付属しているライブラリに含まれている決定派生物のいずれも使用できません。つまり、基本的にmovs、cmps、loops、jumpsです。これが私が得たものです。以前に20個の乱数の最初の1つを並べ替えていましたが、混乱して死に、もう何をしているのかわかりません。挿入ソートメソッドに到達するとクラッシュします。助けてください。

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
4

1 に答える 1

1

コードを詳しく調べていませんが、配列へのポインターとして、および配列からの値の 1 つを保持するという 2 つの異なる目的で同時にInsertionSort使用しているようです。edx他に何も問題がなくても、これは確かに壊れます。

つまり、最初にInsertionSort言うとmov edx, OFFSET myArray、それは配列へのポインターです。それから数行後、mov edx, [eax-4]-- おっと、いや、それは配列からの値です。そして、数行後に再びcmp eax, [edx]-- ああ、いや、今度は再び配列へのポインタです。

おそらく、その最後の指示は何かである必要がありますcmp edx, [eax]か?eaxここでは配列へのポインタのように見えるからです。

于 2011-04-06T23:26:52.387 に答える