汇编-冒泡排序

冒泡排序程序实现:使数组的数从大到小的顺序排序

data segment
	a	dw	20 dup(?)		;20个数据比较
data ends

code segment
	assume cs:code, ds:data
main proc far

start:
	;set up stack for return		;保留现场,数据入栈
	push	ds
	sub	ax, ax
	push	ax							;??

	mov	ax, data
	mov	ds, ax

;program started--------------------------------------------------

	mov	cx, 20		;n个数据需要比较的最大次数=n(n+1)/2
	dec	cx

loop1:					
	*mov	di, cx*
	mov	bx, 0
	mov cx,19
*loop2:
	mov	ax, a[bx]
	cmp	ax, a[bx+2]
	jge	continue
	xchg	ax, a[bx+2]
	mov	a[bx], ax

continue:
	add	bx, 2
	loop	loop2*
	*mov	cx, di*
	loop	loop1

	ret
main endp

code ends
	end start

你可能感兴趣的:(汇编)