⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sort.asm

📁 how to sort in asm language
💻 ASM
字号:
datarea segment
;	max dw 0
	grade dw 50 dup(?)
	count dw ?
	item  dw ?
	a db ?
	b dw ?
	mess1 db 'Input data:','$'
	mess2 db 13,10,'Input error!',13,10,'$'
	mess3 db 13,10,'$'
	mess4 db '1------bubblesort',13,10,'2------insertionsort',13,10,'Your choice:','$'
	mess5 db '1------ascend',13,10,'2------descend',13,10,'Your choice:','$'
	
datarea ends
;***************************************************************
prognam segment
;---------------------------------------------------------------
main proc far
	assume cs:prognam,ds:datarea
start:
	push ds
	sub  ax,ax
	push ax

	mov  ax,datarea
	mov  ds,ax

	call input
	lea dx,mess4
	mov ah,09h
	int 21h
	
	mov ah,01h
	int 21h
	call crlf
	sub al,'0'
	cmp al,2
	jz sort2
	call bubblesort
	jmp output1
sort2:	
	call insertionsort
output1:
	call output
	ret
main endp
;---------------------------------------------------------------
input proc near
	lea dx,mess1
	mov ah,09
	int 21h
	
	mov count,0
	mov si,0
enter:
	call decibin
	inc  count
	cmp  dl,','
	je   store
	cmp  dl,13
	je   exit2
	jne  error
store:
	mov  grade[si],bx
	add  si,2
	jmp  enter
error:
	lea  dx,mess2
	mov  ah,09
	int  21h
exit2:
	mov  grade[si],bx
	call crlf
	ret
input endp
;---------------------------------------------------------------
bubblesort proc near
	
	mov si,count
init:
	mov bx,1
	dec si
	jz sorted
	mov cx,si
	mov di,0
next:
	
	mov ax,grade[di]
	cmp grade[di+2],ax
	jae cont
	xchg grade[di+2],ax
	mov grade[di],ax
	sub bx,bx
cont:	
	add di,2
	loop next
	cmp bx,0
	je init
sorted:
	 
	ret
bubblesort endp

;---------------------------------------------------------------
insertionsort proc near
	mov si,2
	mov cx,count
	dec cx
	
loop1:
	mov ax,grade[si]
	mov item,ax
	mov di,si
	sub di,2
loop2:	
	mov ax,item
	cmp grade[di],ax
	jl  back
	mov ax,grade[di]
	mov grade[di+2],ax
	sub di,2
	cmp di,0
	
	jge loop2
back:	
	mov ax,item
	mov grade[di+2],ax
	add si,2
	dec cx	
	jnz loop1
	ret
insertionsort endp


;---------------------------------------------------------------
output proc near
	lea dx,mess5
	mov ah,09h
	int 21h 
 	
	mov ah,01h
	int 21h
	sub al,'0'
	mov a,al
	call crlf
	cmp a,02d
	je  descend
ascend:		

	mov di,count
	mov si,0
	jmp nexta
descend:	
	mov si,count
	dec si
	shl si,1
	mov di,count
	
nexta:
	mov bx,grade[si]
	call binidec
	mov dl,','
	mov ah,02
	int 21h
	cmp a,2
	jz subsi
	add si,2
	jmp exita
subsi:	sub si,2
exita:	dec di
	jnz nexta
	call crlf
	ret

output endp
;---------------------------------------------------------------
decibin proc near
	mov bx,0
newchar:
	mov ah,1
	int 21h
	mov dl,al
	sub al,30h
	jl  exit1
	cmp al,9d	
	jg exit1
	cbw
	xchg ax,bx
	mov cx,10d
	mul cx
	xchg ax,bx
	add bx,ax
	jmp newchar
exit1: 	ret
decibin endp
;------------------------------------------------------------------
binidec proc near
	push bx
	push cx
	push si
	push di
	mov cx,100d
	call dec_div
	mov cx,10d
	call dec_div
	mov cx,1d
	call dec_div
	pop di
	pop si
	pop cx
	pop bx
	ret
binidec endp

;------------------------------------------------------------------
dec_div proc near
	mov ax,bx
	mov dx,0
	div cx
	mov bx,dx
	cmp al,0
	jz exit3
	mov dl,al
	add dl,30h
	mov ah,02h
	int 21h
exit3:	ret
dec_div endp

;------------------------------------------------------------------
crlf proc near
	mov dl,0ah
	mov ah,02h
	int 21h

	mov dl,0dh
	mov ah,02h
	int 21h
	
	ret
crlf endp
;------------------------------------------------------------------
prognam ends
;******************************************************************
	end start





























⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -