使用emu8086(汇编程序)对数组进行排序 [英] Sort an array with emu8086 (Assembler)

查看:604
本文介绍了使用emu8086(汇编程序)对数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to type and array (only with numbers) and when a introduce it, it must be sorted in a second one. All I have to do is to implement the PROC which has to sort them. My problem is that I don't know how, because the only thing I have achieved is to copy the first one into the second one. Thanks for your help and sorry for my English.

;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

推荐答案

还有另一种分类方法,称为选择排序,在这里:

There is another way of Sorting know as Selection Sort and here its:

数据段定义为:

 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    

这篇关于使用emu8086(汇编程序)对数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆