【问题标题】:Sort an array with emu8086 (Assembler)使用emu8086(汇编程序)对数组进行排序
【发布时间】:2017-09-03 18:06:27
【问题描述】:

我必须键入和排列(仅使用数字),并且在引入它时,必须在第二个中对其进行排序。我所要做的就是实现必须对它们进行排序的 PROC。我的问题是我不知道怎么做,因为我唯一做到的就是将第一个复制到第二个中。感谢您的帮助,对不起我的英语。

;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

【问题讨论】:

  • 冒泡排序的一个例子是here

标签: arrays sorting assembly x86 emu8086


【解决方案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    

【讨论】:

  • 每次找到一个新的候选元素时都会进行大量的交换。这几乎不再是 SelectionSort 了。只需记录您在哪里找到元素并在最后与最终最大值交换。此外,使用atomic xchg (i.e. xchg with a memory operand) 会使这比它需要的慢得多。此外,jna continueja / jmp 更有意义。或者更好的是,为比较优化的分支结构是错误的并且不需要交换。
  • 您不需要 CPU 与xchg dl,bl 交换寄存器,只需存储相反的寄存器即可。另外,你已经有了bl = elements[si],所以你不需要再次加载它。 (但我猜你需要mov bl,dl 指令)
  • @Peter Cordes 我写代码时有点着急,所以没有时间进行优化。不过我还是很着急,因为我要准备后天的考试。
  • 顺便说一句,与新的 min-candidate 交换不是选择排序,但我最近发现它有一个名称:JumpDown Sort。它介于 BubbleSort 和 SelectionSort 之间。见Bubble Sort: An Archaeological Algorithmic Analysis。 (还有我的 19-byte implementation of JumpDown Sort 用于 x86-16/32/64,对 16 位或 32 位整数或字节进行排序。)另外,我的有 cmets,你的没有......
  • @PeterCordes 它与选择排序有何不同?在选择排序中,我们计算最小元素并将其放在第一个位置(如果排序是按升序排列的),这正是我的代码所做的,所以它与选择排序有什么不同?
猜你喜欢
  • 1970-01-01
  • 2021-08-20
  • 2015-09-19
  • 2020-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-13
相关资源
最近更新 更多