2011-04-06 74 views
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,循环和跳 这就是我得到的。我已经把它排序了20个随机数中的第一个,但我已经把自己弄糊涂了,不知道我在做什么。它在到达插入排序方法时崩溃。请帮助。

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 
+2

一个勇敢的灵魂是谁去“这是171汇编程序的行,怎么了? :-)只是在说'... – corsiKa 2011-04-06 23:29:30

回答

1

我没有详细研究你的代码,但我注意到,InsertionSort好像是用edx一次为两个不同的目的:作为一个指针到数组,并保持在该值中的一个阵列。即使没有其他错误,这肯定会破裂。

所以,在InsertionSort的开头你说mov edx, OFFSET myArray - 它是一个指向数组的指针。然后,几行后,mov edx, [eax-4] - 哎呀,不,这是来自阵列的值。再后面几行,cmp eax, [edx] - 哦,不,现在它又是一个指向数组的指针。

也许最后的指令应该是cmp edx, [eax]什么的?因为eax似乎是这里的数组指针。

+0

我已经解决了你提到的问题,但它不能解决我的问题。我也尝试在cmp中使用[eax],并且它似乎会引发错误。此外,有两个寄存器开始与该数组的偏移量,但我需要偏移量稍后再检查,我没有离开该数组的边界。看起来更有点像现在这样:我 L6: \t \t CMP ESP,ESI \t \t \t \t \t \t \t \t JG endinner MOV EBP,[EAX-4] MOV [EAX],EBP 子EAX ,4 \t \t CMP EAX,EDX \t \t JNG endinner \t \t \t \t \t \t \t \t MOV ESP,[EAX-4] \t \t环L6 – michelle 2011-04-06 23:48:50

+0

我不认为这是调试只给出了信息的代码是可行的,它“看起来更有点像这样”!您能否更新问题中的代码以反映您所做的任何修复? – 2011-04-06 23:56:59

+0

(但它听起来好像在你真正设置它之前使用'esp'。) – 2011-04-06 23:59:46