首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在程序集中插入排序

在程序集中插入排序
EN

Stack Overflow用户
提问于 2011-04-07 07:18:52
回答 1查看 8.1K关注 0票数 1

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

代码语言:javascript
复制
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个随机数。我不能使用我们的书附带的库中的决策导数。所以基本上就是mov,cmp,循环和跳跃,这就是我得到的。我之前让它对20个随机数中的第一个进行排序,但我把自己搞得糊里糊涂的,不知道自己在做什么。当它到达插入排序方法时,它会崩溃。请帮帮忙。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2011-04-07 07:26:52

我没有详细研究您的代码,但是我注意到InsertionSort似乎同时使用edx来实现两个不同的目的:作为指向数组的指针,以及保存数组中的一个值。即使没有其他错误,这也肯定会打破这一点。

所以,在InsertionSort的开头,你会说mov edx, OFFSET myArray --它是一个指向数组的指针。然后,几行之后,mov edx, [eax-4] --哦,不,它是数组中的一个值。再过几行,cmp eax, [edx] --哦,不,现在又是指向数组的指针了。

也许最后一条指令应该是cmp edx, [eax]之类的?因为在这里,eax看起来确实是指向数组的指针。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5574106

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档