注册 登录
编程论坛 汇编论坛

汇编 选择排序

xiaoxun_1122 发布于 2010-12-28 14:29, 656 次点击
程序代码:
;有一个首地址为A的N字无序数组,编制程序采用选择排序法使该数组中的数按照从大到小的次序排序输出
DATAS SEGMENT
a dw 1,7,2,9,4,8,7,6,3
mess1 db 'the array preinstalled is:12,7,22,19,14,48,37,33,66,31$'
mess2 db 0ah,0dh,'Already sorted array is:','$'
DATAS ENDS
CODES SEGMENT
    ASSUME CS:CODES,DS:DATAS
START:
    MOV AX,DATAS
    MOV DS,AX
    lea dx,mess1
    mov ah,09h
    int 21h
    lea dx,mess2
    mov ah,09h
    int 21h
    mov ax,mess1-a
    mov dl,2
    div dl
    cbw
    xor ah,ah
    mov cx,ax
    mov si,0
loop1:
    mov di,cx
    inc si
    sub si,1
    push si
    mov bx,si
loop2:
    mov ax,a[si+2]  
    cmp ax,a[bx]
    jb continue
    add si,2
    mov bx,si
continue:
    add si,2
    loop loop2   
    pop si
    mov ax,a[si]
    xchg ax,a[bx]
    mov a[si],ax
    mov dx,a[si]
    add dx,30h
    mov ah,02h
    int 21h
    sub di,1
    mov cx,di
    add si,2
    loop loop1
    MOV AH,4CH
    INT 21H
CODES ENDS
    END START
求教 为什么会错误
1 回复
#2
xiaomarn2010-12-29 10:42
程序代码:
DATAS SEGMENT
a dw 1,7,2,9,4,8,7,6,3
mess1 db 'the array preinstalled is:12,7,22,19,14,48,37,33,66,31$'
mess2 db 0ah,0dh,'Already sorted array is:','$'
DATAS ENDS
CODES SEGMENT
    ASSUME CS:CODES,DS:DATAS
START:
    MOV AX,DATAS
    MOV DS,AX
   

    lea dx,mess1
    mov ah,09h
    int 21h
    lea dx,mess2
    mov ah,09h
    int 21h
    mov ax,offset mess1-offset a
    mov dl,2
    div dl
    cbw
    xor ah,ah
    mov cx,ax
   

    mov si,0
loop1:
    mov di,cx
  

    push si
    mov bx,si
    dec cx
    or cx,cx
    jz ze
loop2:
    mov ax,a[si+2]

    cmp ax,a[bx]
    jb continue
    lea bx,[si+2]
continue:
    add si,2
    loop loop2
   

    pop si
    mov ax,a[si]
    xchg ax,a[bx]
    mov a[si],ax
ze:
    mov dx,a[si]
    call dishex
    ;dec di
    mov cx,di
    inc si
    inc si
    loop loop1
   

    MOV AH,4CH
    INT 21H
   

dishex proc near
    push cx
    mov cx,0404h
print:
    rol dx,cl
    push dx
    and dl,0fh
    cmp dl,0ah
    jb next
    add dl,7
next:
    add dl,30h
    mov ah,2
    int 21h
    pop dx
    dec ch
    jnz print
    mov dl,' '
    mov ah,2
    int 21h
    pop cx
    ret
dishex endp
CODES ENDS
    END START

1