write a program to sort a array in ascending order in Assembly Language  

Code:

 

name "Assending order array"


.model small

.stack 100h


.data

msg db 0dh,0ah,0ah,"Number List : $"

msg1 db 0dh,0ah," Sort      : $"

msg3 db 0dh,0ah,0ah,"NAME: Usama Qasim $"

msg33 db 0dh,0ah,"Roll: 17251519-015 $"

msg4 db 0dh,0ah,0ah,"NAME: Umair Idrees $"

msg44 db 0dh,0ah,"Roll: 17251519-034 $"

msg5 db 0dh,0ah,0ah,"NAME: Sheraz Ahmed $"

msg55 db 0dh,0ah,"Roll: 16171519-072 $"



array db 5,3,9,0,2,6,1,7,8,4


.code

main proc

mov ax, @data 

mov ds, ax


mov bx, 10 ; set bx=10


lea dx,msg4 ; load and display user1 name

mov ah,09

int 21h


lea dx,msg44 ; load and display user1 roll

mov ah,09

int 21h

lea dx,msg3    ; load and display user2 name

mov ah,09

int 21h

lea dx,msg33    ; load and display  user2 roll

mov ah,09

int 21h

lea dx,msg5     ; load and display  user3 name

mov ah,09

int 21h


lea dx,msg55    ; load and display  user3 roll

mov ah,09

int 21h


lea dx, msg ; load and display the string msg

mov ah, 9

int 21h


lea si, array ; set si=offset address of array


call arr_disp ; call the procedure arr_disp


lea si, array ; set si=offset address of the array


call sorting ; call the procedure sorting


lea dx, msg1 ; load and display the string msg1

mov ah, 9

int 21h


lea si, array ; set si=offset address of array


call arr_disp ; call the procedure arr_disp


mov ah, 4ch 

int 21h

main endp




arr_disp proc



push ax ; push ax onto the stack

push cx ; push cx onto the stack

push dx ; push dx onto the stack


mov cx, bx ; set cx=bx


zero: ; loop zero

xor ah, ah ; encrapt values

mov al, [si] ; set al=[si]


call outdec ; call the fuction


mov ah, 2 

mov dl, 20h ; set dl=20h

int 21h ; print a character


inc si ; set si=si+1

loop zero ; jump to label zero while cx!=0


pop dx ; pop a value from stack into dx

pop cx ; pop a value from stack into cx

pop ax ; pop a value from stack into ax


ret ; return control to the calling procedure

arr_disp endp


;

;sorting

;

sorting proc



push ax ; push ax onto the stack

push bx ; push bx onto the stack

push cx ; push cx onto the stack

push dx ; push dx onto the stack

push di ; push di onto the stack


mov ax, si ; set ax=si

mov cx, bx ; set cx=bx

dec cx ; set cx=cx-1


Loop2: ; loop label

mov bx, cx ; set bx=cx


mov si, ax ; set si=ax

mov di, ax ; set di=ax

inc di ; set di=di+1


Loop1: ; loop label

mov dl, [si] ; set dl=[si]


cmp dl, [di] ; compare dl with [di]            

jng Swap ; jump to label Swap if dl>=[di]    //tricky part


xchg dl, [di] ; set dl=[di], [di]=dl

mov [si], dl ; set [si]=dl


Swap: ; jump label

inc si ; set si=si+1

inc di ; set di=di+1


dec bx ; set bx=bx-1

jnz Loop1 

loop Loop2 


pop di ; pop a value from stack into di

pop dx ; pop a value from stack into dx

pop cx ; pop a value from stack into cx

pop bx ; pop a value from stack into bx

pop ax ; pop a value from stack into ax  



ret ; return

sorting endp


outdec proc



push bx ; push bx onto the stack

push cx ; push cx onto the stack

push dx ; push dx onto the stack


xor cx, cx ; decrept data

mov bx, 10 ; 


zoom: ; loop label

xor dx, dx 

div bx 

push dx ; push dx onto the stack

inc cx ; increment cx

or ax, ax ; take or of ax with ax

jne zoom ; jump to label zoom


mov ah, 2 ; set output function


Show_Data: ; loop label

pop dx ; pop a value from stack to dx

or dl, 30h ; convert decimal to ascii code

int 21h ; print a character

loop Show_Data ; jump to label Show_Data if cx!=0


pop dx ; pop a value from stack into dx

pop cx ; pop a value from stack into cx

pop bx ; pop a value from stack into bx


ret ; return control to the calling procedure

outdec endp


end main


Output: