Microprocessor 8086 Assembly - SORTING OF N NUMBERS IN ASCENDING ORDER

SORTING OF N NUMBERS IN ASCENDING ORDER

;PERFORM SORTING OF N NUMBERS IN ASCENDING ORDER

.model small
.stack 20h
.data
 msg1 db 'Enter the LIMIT:-$'
 msg2 db 'Enter the numbers..$'
 msg3 db 'The sorted array is ...$'
 n dw 0h
 A db 25 dup(0)
 tmp1 dw 0h
 tmp2 dw 0h
 flag1 db 0h
 flag2 db 1h
 ten dw 0Ah

.code
newl proc
   mov ah,02h
   mov dl,0dh
   int 21h
   mov dl,0ah
   int 21h
   ret
newl endp

 main proc
 mov ax,@data
 mov ds,ax
 call newl
 lea dx,msg1
 call message
 call input
 mov n,bx
 call newl
 lea dx,msg2
 call message
 call array
 call newl
 mov flag1,01h
 call nsort
 lea dx,msg3
 call message
 call newl
 call array
 mov ah,04ch
 int 21h
 main endp

 array proc
 call newl
 xor si,si
 mov cx,n
we:
 mov tmp1,cx
 cmp flag1,01h
 je t
 call newl
 call input
 mov a[si],bl
 jmp s
t:
 cmp cx,n
 je d
 call comma
d:
 xor bx,bx
 mov bl,a[si]
 mov tmp1,cx
 call output
s:
  mov cx,tmp1
  inc si
  loop we
 ret
 array endp

nsort proc
  mov dx,n
  dec dx
 b:
  cmp flag2,0h
  je ending
  mov flag2,0h
  xor cx,cx
 g:
  mov si,cx
  mov al,a[si]
  cmp al,a[si+1]
  jg xc
c:
  inc cx
  cmp cx,dx
  je b
  jmp g
xc:
  xchg al,a[si+1]
  mov a[si],al
  mov flag2,01h
  jmp c
ending:
  ret
  nsort endp
  
  input proc
   xor bx,bx
begin :
   mov ah,01h
   int 21h
   cmp al,0dh
   je exit
   sub al,30h
   xor ah,ah
   xchg ax,bx
   mul ten
   add bx,ax
   jmp begin
exit:
   ret
input endp

  output proc
   xor cx,cx
   xor dx,dx
   mov ax,bx
repeat:
   xor dx,dx
   div ten
   push dx
   inc cx
   cmp ax,0
   jne repeat
   mov ah,02h
disp:
   pop dx
   add dl,30h
   int 21h
   loop disp
   ret
output endp

message proc
mov ah,09h
int 21h
ret
message endp

 comma proc
 mov dl,02ch
 mov ah,02h
 int 21h
 ret
 comma endp
     end main

Comments

Popular posts from this blog

Introduction to C++ Programming Language

The Assembly Process Of 8086 Microprocessor-Two Pass Assembling Steps

Installing 8086 Assembler on a Computer