Monday, 6 February 2012

Masm program to sort n numbers


printmsg macro msg
mov ah,09h
lea dx,msg
int 21h
endm

printnum macro reg
mov dl,reg
add dl,30h
mov ah,02h
int 21h
endm

calcnum macro regx
mov ax,regx
mov dh,100
div dh
mov n,ah
aam
mov dh,al
printnum ah
printnum dh
mov al,n
aam
mov dh,al
printnum ah
printnum dh
endm


data segment
num db 0
array db 50 dup(0)
n db 0



cr equ 0dh
msg0 db "Enter a limit(Ex:05)$"
msg1 db "Enter the nos(Ex:06)$"
msg2 db 0ah,0dh,"After sorting$"
msg4 db 0ah,"$"
msg5 db 0ah,0dh,"Before sorting$"
data ends

code segment
assume cs:code,ds:data

start:
mov ax,data
mov ds,ax
mov cl,00
printmsg msg0
mov ah,01h
int 21h
sub al,30h
mov n,al
mov ah,00
mov dh,10
mul dh
mov n,ax
mov ah,01h
int 21h
sub al,30h
mov ah,00
add ax,n

mov cl,ax
mov num,cl
printmsg msg4
printmsg msg1
lea si,array
l1:

mov ah,01h
int 21h
sub al,30h
mov ah,00
mov ch,10
mul ch
mov n,ax
mov ah,01h
int 21h
sub al,30h
mov ah,00
add ax,n
mov [si],ax

inc si
dec cl
printmsg msg4
cmp cl,00
jne l1



printmsg msg4
mov cl,num
mov ch,00
printmsg msg5
printmsg msg5
lea si,array
t1:
printmsg msg4
cmp cl,ch
je t2

mov ah,00
mov al,[si]
calcnum ax

inc ch
inc si
jmp t1
t2:
printmsg msg4
mov cl,num
mov bh,00
mov bl,00
mov dl,num
dec dl
lea si,array
i1:

cmp bh,cl
je stop
mov di,si
inc di
mov bl,bh
inc bl
j1:
cmp bl,cl
je jstop
mov ch,[si]
cmp ch,[di]
jc k1
xchg ch,[di]
mov [si],ch
k1:
inc bl
inc di
jmp j1
jstop:
inc si
inc bh
jmp i1
stop:


mov cl,num
lea si,array
printmsg msg4
printmsg msg2
q1:
printmsg msg4
mov ah,00
mov al,[si]
calcnum ax
dec cl
inc si
cmp cl,00
jne q1
mov ah,4ch
int 21h
code ends
end start




1 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More