Chinaunix首页 | 论坛 | 博客
  • 博客访问: 20274
  • 博文数量: 17
  • 博客积分: 730
  • 博客等级: 军士长
  • 技术积分: 175
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-24 12:09
文章分类

全部博文(17)

文章存档

2010年(17)

我的朋友
最近访客

分类: 嵌入式

2010-01-24 13:38:06

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 2编程,对下列数据进行由大到小的排序。
; 19H,16H,18H,15H,17H,10H,13H
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DATAS SEGMENT
   buf db 19H,16H,18H,15H,17H,10H,13H,08h,07h,21h,23h,29h,31h
   count equ $-buf
   before_sort db "before sorting ,the number sequence in buf is:"
   db 0ah,0dh,'$'
  
   after_sort   db "after sorting  ,the number sequence in buf is:"
   db 0ah,0dh,'$'
DATAS ENDS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
STACKS SEGMENT
    
STACKS ENDS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
CODES SEGMENT
    ASSUME CS:CODES,DS:DATAS,SS:STACKS
    
    
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
; 打印数字子程序
;printvalue  dx = num    
show_asc proc
push ax
push bx
push cx
push dx
mov bx,dx
mov cx,2
mov ah,02h
lop:
mov dl,bl
int 21h
push cx
mov cl,8
shr bx,cl
pop cx
dec cx
jnz lop
mov ah,02h
mov dl,'h'
int 21h
pop dx
pop cx
pop bx
pop ax
ret
show_asc endp
;ax = 要输出的数字  dx=格式化的数字
;把一个两位数的每一个ascii打印出来。
show_num proc
push ax
push bx
push cx
push dx
mov cx,2h
mov bx,ax
mov dx,0
lop2:
mov bl,al
and bx,0fh
add bl,30h
cmp bl,'9'
jng next
add bl,7h
next:
or dx,bx
dec cx
jz  ok
push cx
mov cl,4
shl dx,cl
shl dx,cl  ;由于一个字符要占8个bit,所以dx往左移8位
shr ax,cl  ;而ax移动4位
pop cx
jmp lop2
ok:
call show_asc
pop dx
pop cx
pop bx
pop ax
ret
 
show_num endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;                  换行
change_line proc
push ax
push dx
mov ah,2h
mov dl,0ah
    int 21h
    mov dl,0dh
    int 21h
    pop dx
    pop ax
ret
change_line endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
; 打印出一段内存空间里面的数据(字节)
print_buf_bit:
push ax
push bx
push cx
push dx
lea dx,before_sort
cmp ax,1
jne  after
lea dx,after_sort
after:
mov ah,09h
int 21h
mov cx,count
lea bx,buf
lop_print_bit:
mov  ax,[bx]
and ax,0ffh
call show_num
inc bx
mov dl,' '
mov ah,02h
int 21h
loop lop_print_bit
pop dx
pop cx
pop bx
pop ax
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
; 对一段内存中字节进行排序
;;冒泡法实现
sort_buf_bit proc
push ax
push bx
push cx
push dx
push si
push di
mov cx,0
sort_lop_1:
mov bx,count-2
cmp cx,bx ;cx 控制循环多少大圈
jg over_sort
mov dx,0 ;dx控制 每一次内循环均从开头来循环,每一个小循环,最小的数字就移动
;到合适的位置
;下面是冒泡法的内循环
sort_lop_2:
mov bx,count-2 ;每一个小循环,均与该小循环所处的大循环次数有关
;所以,要动态减少
sub bx,cx
cmp dx,bx
jg next_sort
lea si,buf
add si,dx
mov di,si
inc di
mov al,[si]  ;从内存中取数
mov ah,[di]  ;取出来相邻的两个数字
cmp al,ah
jg next_lop_2;如果前一个数大于后一个数,不用交换
mov [di],al;交换下顺序,直接写进内存了。
mov [si],ah
next_lop_2:
inc dx
jmp sort_lop_2
next_sort:
inc cx
jmp sort_lop_1
over_sort:
pop di
pop si
pop dx
pop cx
pop bx
pop ax
ret
sort_buf_bit endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;           主程序                           ;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
START:
    MOV AX,DATAS
    MOV DS,AX
mov ax,0
call print_buf_bit
call change_line
call change_line
   call sort_buf_bit
      mov ax,1
    call print_buf_bit
    MOV AH,4CH
    INT 21H
CODES ENDS
    END START

阅读(363) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~