; PROGRAM: SORT AN ARRAY
read macro num
mov ah, 01h
int 21h
mov bh, al
int 21h
mov bl, al
sub bh, 30h
mov al, 0ah
mul bh
sub bl, 30h
add al, bl
mov num, al
endm
print macro s
mov ah, 09h
mov dx, offset s
int 21h
endm
data segment
msg db 'Enter Limit: $'
dot db '. $'
comma db ', $'
enter db 'Enter elements: $'
arris db 'Sorted elements: $'
nl db 0dh,0ah,'$'
limit db 00h
cnt dw 00h
arr db 50 dup('$')
num db 3 dup('$')
ele db ?
small db ?
data ends
code segment
assume cs:code, ds:data
start:
mov ax, data
mov ds, ax
mov es, ax
print msg
read limit
mov cl, limit
mov ch, 00h
print nl
print enter
mov si, offset arr
READARR:
read ele
mov al, ele
mov [si], al
inc si
print comma
loop READARR
print nl
;LOGIC TO SORT THE ARRAY SELECTION SORT
mov al, 00h ;a=0
OUTER_LOOP:
mov bh, limit
dec bh
cmp al, bh ;while(a jge EXIT_OUTER
mov ah, 00h
mov bp, ax ;bp(smallest)=ax
mov bl, al ;b=a+1
mov bh, 00h
inc bl
mov si, offset arr
INNER_LOOP:
cmp bl, limit ;while(b jge EXIT_INNER
mov dh, [bp][si] ;dh<-arr nbsp="" p="" smallest=""> mov dl, [bx][si] ;dl<-arr b="" p=""> cmp dh, dl
jle POSCORRECT ;if(arr[a]>arr[b])
mov bp, bx ;bp(smallest)=b
POSCORRECT:
inc bl ;b=b+1
jmp INNER_LOOP ;END WHILE
EXIT_INNER:
cmp ax, bp ;if(smallest(bp)!=a)
je SKIP_SWAP ;swap arr[a] with arr[small]
mov si, offset arr
mov dl, [bp][si]
mov bx, ax
mov dh, [bx][si]
mov [bx][si], dl
mov [bp][si], dh
SKIP_SWAP:
inc al ;a=a+1
jmp OUTER_LOOP ;END WHILE
EXIT_OUTER:
print arris
mov cl, limit
mov ch, 00h
mov di, offset arr
PRINTARR:
mov al, [di]
mov ah, 00h
mov si, offset num
mov cnt, cx
call hex2ascii
print num
print comma
inc di
mov cx, cnt
loop PRINTARR
mov ah, 4ch
int 21h
hex2ascii proc near
mov cx, 0000h
REPDIV:
mov dx, 0000h
mov bx, 000ah
div bx
add dl, 30h
push dx
inc cx
cmp ax, 000ah
JGE REPDIV
cmp al, 00h
je SKIP0
add al, 30h
mov [si], al
inc si
SKIP0:
SAVEASCI:
pop dx
mov [si], dl
inc si
loop SAVEASCI
STOP1:
mov [si], '$'
ret
hex2ascii endp
code ends
end start
**************************
OUTPUT
Z:\S5IT\masm>array_sort
Enter Limit: 07
Enter elements: 12, 90, 67, 03, 06, 23, 58,
Sorted elements: 3, 6, 12, 23, 58, 67, 90,
**************************-arr>-arr>
read macro num
mov ah, 01h
int 21h
mov bh, al
int 21h
mov bl, al
sub bh, 30h
mov al, 0ah
mul bh
sub bl, 30h
add al, bl
mov num, al
endm
print macro s
mov ah, 09h
mov dx, offset s
int 21h
endm
data segment
msg db 'Enter Limit: $'
dot db '. $'
comma db ', $'
enter db 'Enter elements: $'
arris db 'Sorted elements: $'
nl db 0dh,0ah,'$'
limit db 00h
cnt dw 00h
arr db 50 dup('$')
num db 3 dup('$')
ele db ?
small db ?
data ends
code segment
assume cs:code, ds:data
start:
mov ax, data
mov ds, ax
mov es, ax
print msg
read limit
mov cl, limit
mov ch, 00h
print nl
print enter
mov si, offset arr
READARR:
read ele
mov al, ele
mov [si], al
inc si
print comma
loop READARR
print nl
;LOGIC TO SORT THE ARRAY SELECTION SORT
mov al, 00h ;a=0
OUTER_LOOP:
mov bh, limit
dec bh
cmp al, bh ;while(a
mov ah, 00h
mov bp, ax ;bp(smallest)=ax
mov bl, al ;b=a+1
mov bh, 00h
inc bl
mov si, offset arr
INNER_LOOP:
cmp bl, limit ;while(b
mov dh, [bp][si] ;dh<-arr nbsp="" p="" smallest=""> mov dl, [bx][si] ;dl<-arr b="" p=""> cmp dh, dl
jle POSCORRECT ;if(arr[a]>arr[b])
mov bp, bx ;bp(smallest)=b
POSCORRECT:
inc bl ;b=b+1
jmp INNER_LOOP ;END WHILE
EXIT_INNER:
cmp ax, bp ;if(smallest(bp)!=a)
je SKIP_SWAP ;swap arr[a] with arr[small]
mov si, offset arr
mov dl, [bp][si]
mov bx, ax
mov dh, [bx][si]
mov [bx][si], dl
mov [bp][si], dh
SKIP_SWAP:
inc al ;a=a+1
jmp OUTER_LOOP ;END WHILE
EXIT_OUTER:
print arris
mov cl, limit
mov ch, 00h
mov di, offset arr
PRINTARR:
mov al, [di]
mov ah, 00h
mov si, offset num
mov cnt, cx
call hex2ascii
print num
print comma
inc di
mov cx, cnt
loop PRINTARR
mov ah, 4ch
int 21h
hex2ascii proc near
mov cx, 0000h
REPDIV:
mov dx, 0000h
mov bx, 000ah
div bx
add dl, 30h
push dx
inc cx
cmp ax, 000ah
JGE REPDIV
cmp al, 00h
je SKIP0
add al, 30h
mov [si], al
inc si
SKIP0:
SAVEASCI:
pop dx
mov [si], dl
inc si
loop SAVEASCI
STOP1:
mov [si], '$'
ret
hex2ascii endp
code ends
end start
**************************
OUTPUT
Z:\S5IT\masm>array_sort
Enter Limit: 07
Enter elements: 12, 90, 67, 03, 06, 23, 58,
Sorted elements: 3, 6, 12, 23, 58, 67, 90,
**************************-arr>-arr>
0 comments:
Post a Comment