         ;-------------------------------
;MACRO HIEN MOT XAU KY TU |
;KET THUC BANG '$' RA MAN HINH |
;-------------------------------
HienString MACRO xau
push AX
push DX
mov  DX,offset xau
mov  AH,9
int  21h
pop  DX
pop  AX
ENDM  
.model small
.stack 100h
.data
  m1 db 13,10,'nhap so luong chu so:$'
  m2 db 13,10,'a[$'
  m3 db ']=$'
  m4 db 13,10,'cac so vua vao la:$'
  m5 db 13,10,'day sp da sap xep:$'
  slcs dw ?
  index dw ?
  i dw ?
  a dw 100 dup(?)
  m6 db '  $'
.code
 ps:
   mov ax,@data
   mov ds,ax
   hienstring m1   ; hien thong bao'nhap so luong '
   call vao_so_n
   mov slcs,ax
   mov index,ax
   mov cx,ax
   mov bx,offset a
   mov i,0
 l1:
   hienstring m2
   mov ax,i
   call hien_so_n
   hienstring m3
   call vao_so_n
   mov [bx],ax
   inc i
   add bx,2
   loop l1
   hienstring m4
   call hien_day
   dec index
 t2:
   mov cx,slcs
   mov bx,offset a
   dec cx
 t3:
   mov ax,[bx]
   mov dx,[bx+2]
   cmp ax,dx
   jl  t4
   mov [bx],dx
   mov [bx+2],ax
 t4:
   add bx,2
   loop t3
   dec index
   jnz t2
   hienstring m5
Page 1sap xep.asm
   call hien_day
  
   mov ah,4ch
   int 21h
  
hien_day proc
  push ax 
  push bx
  push cx
  mov cx,slcs
  mov bx,offset a
hd:
  mov ax,[bx]
  call hien_so_n
hienstring m6
  add bx,2
  loop hd
  pop cx 
  pop bx
  pop ax
  ret
hien_day endp
;==================================================
;Chuong trinh con
;==================================================
VAO_SO_N PROC
push BX
push CX
push DX
push SI
mov  BX,10 ; Mot so hang nhan
xor  CX,CX ; Phan so da vao truoc (luc dau bang 0)
mov  SI,CX ; Co dau cua so (luc dau coi nhu so duong)
   VSN1:
mov  AH,1 ; Cho 1 ky tu tu ban phim
int  21h
cmp  AL,0dh ; Ky tu vua vao la phim Enter
je   VSN3
cmp  AL,'-' ; Ky tu vua vao la dai '-'
jne  VSN2 ; Khong phai dau '-' thi nhay
inc  SI ; con khong thi co dau len 1
jmp  VSN1
    VSN2:
sub  AL,30h ; Chuyen ky tu vua vao tu ma ASCII sang dang so
xor  AH,AH ; AH=0 (AX la so vua vao)
xchg AX,CX ; Doi cheo so vua vao va phan so da vao truoc
mul  BX ; Nhan phan so da vao truoc voi 10
add  CX,AX ; Cong so vua vao voi phan so da vao sau khi nhan 10
jmp  VSN1
   VSN3:
and  SI,SI ; Dung co ZF (Neu SI#0 thi so vao la am)
jz   VSN4 ; So vua vao la duong thi nhay
neg  CX ; Bu 2 gia tri vua vao de duoc so am
   VSN4:
mov  AX,CX
pop  SI
pop  DX
pop  CX
pop  BX
ret
VAO_SO_N ENDP 
;----------------------------
; HIEN NOI DUNG THANH GHI AX |
; RA MAN HINH DANG CO SO 10  |
;----------------------------
HIEN_SO_N PROC
Page 2sap xep.asm
push AX
push BX
push CX
push DX
mov  BX,10 ; BX chua so chia la 10
xor  CX,CX ; So lan cat vao stack (luc dau = 0)
and  AX,AX ; Dung co dau SF
jns  HSN1 ; Neu so can hien la duong thi nhay
push AX ; con khong thi cat AX vao stack
mov  AL,'-' ; Hien dau '-' ra man hinh
mov  AH,0eh
int  10h
pop  AX ; Hoi phuc lai so can hien tu stack vao AX
neg  AX ; Bu 2 de lay gia tri tuyet doi
   HSN1:
xor  DX,DX ; DX=0
div  BX ; DX:AX chia cho BX (10)
add  DX,30h ; Chuyen so du tu dang so ra dang ASCII
push DX ; Cat vao stack
inc  CX ; Tang so lan cat vao stack len 1
and  AX,AX ; Dung co ZF (lieu thuong = 0)
jnz  HSN1 ; Thuong # 0 thi nhay den tiep tuc qua trinh chia
   HSN2: ; Vong lap lay cac gia tri co trong stack hien 
pop  AX
mov  AH,0eh ; Chuc nang hien 1 ky tu dang ASCII o AL ra man hinh
int  10h
loop HSN2
pop  DX
pop  CX
pop  BX
pop  AX
ret
HIEN_SO_N ENDP
 end ps
