2017-04-08 7 views
0

私は型と配列(数字のみ)を導入する必要があります。導入するには、2番目の並べ替えが必要です。ソートしなければならないPROCを実装するだけです。私の問題は、私が達成した唯一のことは、最初のものを2番目のものにコピーすることなので、私は方法がわからないということです。あなたの助けに感謝し、私の英語を残念に思う。配列をemu8086(アセンブラ)で並べ替え

;mov ax, vector[si] 
;mov vector[di], ax 
;this loop copy all elements 
; start code 
Sort_DecreasingOrder: 

cmp si, 0 
mov ax, vector1[si] 

bCompare: 
xor di, di 
mov bx, vector2[di] 
cmp ax, bx 
jge IntroduceBefore 
+1

exampl "バブル"ソートは、[ここ](http://stackoverflow.com/a/26324630/3512216)です。 – rkhb

答えて

1

ソートする別の方法がSelection Sortとして知っていて、ここではそのあり:配列をソートするために

elements db 7,1,0,5,'$' 
size dw $-elements - 1  ; size stores 5 (including '$') so we subtract one now it stores 4 (which is the actual number of elements) 
msg db 10,13,'Unsorted Array: $'   
msg2 db 10,13,'Sorted Array: $' 

手順:

データセグメントは次のように定義され

sort proc  

    mov cx, size  
    mov si, 0 
    mov di, 0 
    mov ax, 1 ; this is done 
    dec cx ; to avoid the last comparison 

    outerLoop:   

     push cx ; store the limit of outerLoop in stack 

     mov bl, elements[si] ; store the element in bl pointed by si             

     mov cx, size ; store the value of size in cx for innerLoop   
     sub cx, ax  ; this is done so that the limit does not proceed the limit of array elements. 

     innerLoop: 

      inc di     

      cmp bl, elements[di] ; compare the if BL is not greater than 
      jna continue   ; content of element pointed by DI if yes then continue otherwise swap the value by executing below statements 

      mov dl, elements[di] ; get the element pointed by DI into DL 
      mov elements[di], bl ; swap the 
      mov elements[si], dl ; elements 
      mov bl, dl    ; store the smallest element in BL        

      continue: 

     loop inner 

     inc ax               

     pop cx ; get the limit of outerLoop 

     inc si ; increment the index of outerLoop to point to the next element 

     mov di, si 

     loop outer       

    return2:  

     ret  

sort endp     

end  
+1

これは、新しい候補要素を見つけるたびに多くのスワップを行います。それはもはやSelectionSortではありません。要素を見つけた場所を記録し、最後に最後の最大値でスワップします。また、[atomic 'xchg'(xchgにメモリオペランドを指定)](http://felixcloutier.com/x86/XCHG.html)を使うと、必要以上に遅くなります。また、 'ja'は' ja'/'jmp'よりも意味があります。あるいは、比較が偽であり、スワップが不要であることを最適化する分岐構造。 –

+1

'xchg dl、bl'とレジスタを入れ替えるのにCPUを必要とせず、逆のレジスタを格納するだけです。また、すでに 'bl' =' elements [si] 'を持っているので、再度ロードする必要はありません。 (しかし、あなたは私が推測する 'mov bl、dl'命令を必要とします) –

+0

@Peter Cordesそのコードを書いたときに私は少し急いでいましたので、最適化のための時間はありませんでした。私はまだ明日の試験の準備が必要なので、まだ急いでいます。 – Ahtisham