%OUT------------------------------------------------------------------------------------------------------------------------------------
%OUT- Accept two arrays, merge them and then sort them in ascending order using any SORTING algorithm
%out- input: Each term of the each of the two arrays is accepted from the user
; Sizes of the arrays are mentioned in the program itself
%out- output: Print the arrays after merging them and reprint after sorting them
%out – author: Ayon and Ariyam
%OUT---------------------------------------------------------------------------------------------------------------------------------------
if1
include macrolib.lby
endif
.model small
.stack
.data
array_size1 equ 4 ;assuming array_size1 to be equal to 3
array1 db array_size1 dup(?) ;reserving bytes = array_size1 for the array1, uninitialised
array_size2 equ 3 ;assuming array_size2 to be equal to 3
array2 db array_size2 dup(?) ;reserving bytes = array_size2 for the array2, uninitialised
merge_size equ 7 ;this will be equal to array_size1+array_size2
ip1 db "Enter terms for 1st array: ","$" ;a request to prompt the user for entering terms of the 1st array
ip2 db "Enter terms for 2nd array: ","$" ;a request to prompt the user for entering terms of the 2nd array
op1 db "After merging: ","$" ;output message
op2 db "After sorting: ","$" ;output message
heading db "***** MERGE & THEN SORT *****","$" ;a heading
.code
main proc
clrscrn ;clearing the screen
mov dx,0105h
cursor ;setting up the cursor
showmsg heading ;displaying heading
mov dx,0303h ;pointing to next line
cursor ;setting up the cursor
input_array array1,array_size1,ip1 ;accept array from user
mov dx,0503h ;pointing to next line
cursor ;setting up the cursor
input_array array2,array_size2,ip2 ;accept array from user
; ============= begin merging ==================================
mov si,array_size1
mov di,00h
mov cx,array_size2
merge: mov ah,array2[di]
mov array1[si],ah ;merge the element from second array into first at the end
inc di
inc si
loop merge
mov dx,0703h ;pointing to next line
cursor ;setting up the cursor
display_array array1,merge_size,op1 ;print the merged array
; ============== begin sorting (bubble sort) ===================
mov cx,merge_size
sub cx,01h
outer: push cx ;saving the current iteration number
mov bx,0001h
;loading bx (displacement for based indexed addressing with displacement)
mov di,00h ;pointer to first array element
inner: mov al,array1[di]
cmp al,array1[di+bx] ;comparing 2 consecutive array elements
jbe continue
swap: mov ah,array1[di+bx]
;performing swap by using two registers ah and al, when succeeding array element is smaller
mov array1[di],ah
mov array1[di+bx],al
continue: inc di ;point to next array element
loop inner ;iterate till the maximum is bubbled down
pop cx ;loader count for outer loop
dec cx
cmp cx,0000h ;short jump not possible
jne outer
mov dx,0903h ;pointing to next line
cursor ;setting up the cursor
display_array array1, merge_size, op2 ;print the merged array after sorting
exit
main endp
end main