2

NASMで数字の挿入ソートを行う必要があります。乱数を生成し、バイナリ形式の数値を含む出力ファイルを生成するファイルがあります。私のプログラムはこれを入力ファイルとしてロードし、挿入ソートを使用してソートされた数値をバイナリ形式で出力する必要があります。

私のコード:

; Template assembler source file

section .text
global _start

_start:
    ; put your code here
    mov eax, 3
    mov ebx, 0
    mov ecx, array
    mov edx, 4*32768
    int 80h
    mov [fileLength], eax   
    shr eax,2
    dec eax
    mov [number], eax



    mov ebx, 1 
    outerloop:
        mov ecx,[array + 4*ebx]
        mov [item],ecx 

        mov ecx,ebx 
        interloop:
        mov edx,ecx 
        dec edx
        mov esi, [array + 4*edx]
        cmp esi,[array + 4*ecx]     
        jb koniec 
            mov eax,[array + 4*edx]
            mov [array + 4*ecx],eax 
        loop interloop
        koniec:

    mov edx,[item]
    mov [array + 4*ecx],edx 

    inc ebx
    cmp ebx,[number] 
    jne outerloop


    mov eax, 4
    mov ebx, 1
    mov ecx, array
    mov edx, [fileLength]
    int 80h



    ; exit to linux
    mov eax,1
    mov ebx,0
    int 80h

; initialized data section
; use directives DB (byte), DW (word), DD (doubleword), DQ (quadword)
section .data

; uninitialized data section
; use directives RESB (byte), RESW (word), RESD (doubleword), RESQ (quadword)
section .bss
    fileLength resd 1   
    number resd 1   

    array resd 32768
    item resd 1

挿入ソートを書くために使用した疑似コード:

for i ← 1 to i ← length(A)-1
   {
     // A[ i ] is added in the sorted sequence A[0, .. i-1]
     // save A[i] to make a hole at index iHole
     item ← A[i]
     iHole ← i
     // keep moving the hole to next smaller index until A[iHole - 1] is <= item
     while iHole > 0 and A[iHole - 1] > item
       {
         // move hole to next smaller index
         A[iHole] ← A[iHole - 1]
         iHole ← iHole - 1
       }
     // put item in the hole
     A[iHole] ← item
   }

ソース: http://en.wikipedia.org/wiki/Insertion_sort

それについての私の知識はかなり少なく、何がうまくいかないのかわかりません。最初のいくつかの数字をソートすることがありますが、残りの数字は正しくソートされません。

4

1 に答える 1

3

行を変更する必要があります

cmp esi, [array + 4*edx]

cmp esi, [item]

の前に削除dec eaxしますouterloop。動作するはずです。

于 2012-11-12T14:45:14.393 に答える