Merge Sorting in Assembly Language

This Program Take 2 Sorted Array merge them so that the final array will also be sorted ..Assembly Program by Programming Seekerz

include irvine32.inc

.data
arr1 byte 10 DUP(?)
arr2 byte 10 DUP(?)
arr3 byte 20 DUP(?)
len1 dword ?
len2 dword ?
len3 dword ?
msg1 byte " Enter 10 Numbers in Array 1: ",0
msg2 byte " Enter 10 numbers in Array 2: ",0
msg3 byte " After Soritng:  ",0
msg4 byte "     ",0

.code
Main Proc
call clrscr
call crlf
mov edx,offset msg1
call writestring
call crlf
mov edx,offset arr1
mov ecx,lengthof arr1
mov esi,0
L1:
call readint
mov arr1[esi],al
inc esi
loop L1

call crlf
mov edx,offset msg2
call writestring
call crlf
mov edx,offset arr2
mov ecx,lengthof arr2
mov esi,0
L2:
call readint
mov arr2[esi],al
inc esi
loop L2

mov ebx,0
mov ecx,0
mov edi,0
mov len1,10
mov len2,10
mov len3,20
.while edi < len3

.if ebx < len1 &&  ecx < len2

mov al,arr2[ecx]

.if arr1[ebx] < al
mov al,arr1[ebx]
mov arr3[edi],al
inc ebx

.else
mov al,arr2[ecx]
mov arr3[edi],al
inc ecx

.endif
inc edi

.elseif ebx == len1

.while edi < len3
mov al,arr2[ecx]
mov arr3[edi],al
inc edi
inc ecx
.endw

.else


.while edi< len3
mov al,arr1[ebx]
mov arr3[edi],al
inc edi
inc ebx
.endw
.endif

.endw

mov edx,offset msg3
call writestring
call crlf
mov edx,offset arr3
mov ecx,lengthof arr3
mov esi,0

L3:
mov edx,offset msg4
call writestring

mov al,arr3[esi]
call writedec
inc esi
loop L3

exit
main endp
end main


0 comments:

Post a Comment

My Instagram