The ArrayMergeSort function

The ArrayMergeSort function merges two arrays into a third array and sorts it in ascending order.

Parameters: DS:AX - first array; ES:BX - second array; DS:CX - third array (result); SI - length of the first array; DI - length of the second array.

Return value: CX - number of elements in the third array.

Flag state: The function does not modify the flags.

Program source code

An example program for the Intel 8086 assembler demonstrating the use of the ArrayMergeSort function to merge and sort two arrays.

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    arrayC  dw  4, 5, 2, 3
    arrClen equ ($-arrayC) / 2
    arrayD  dw  2, 6, 3, 1, 7, 8
    arrDlen equ ($-arrayD) / 2
    arrayB  dw  arrClen + arrDlen dup(0)
    fmtout  db  "[%d] \0"
data ends

code segment public                ; Start of the code segment
    assume  ds:data, cs:code
    include Handyfar.inc           ; Include the file with functions
    include Handymac.inc           ; Include the file with macros

    main proc                      ; Start of the main procedure
        call    SetDataSegment     ; Call the SetDataSegment procedure

        mov     di, arrDlen        ; Copy the value of the arrDlen variable into the di register
        mov     si, arrClen        ; Copy the value of the arrClen variable into the si register
        lea     cx, ds:[arrayB]    ; Load the address of the variable arrayB into the ds:cx register
        lea     bx, es:[arrayD]    ; Load the address of the variable arrayD into the es:bx register
        lea     ax, ds:[arrayC]    ; Load the address of the variable arrayC into the ds:ax register
        call    ArrayMergeSort     ; Call the ArrayMergeSort procedure (ax, bx, cx, si, di) := CX

        lea     si, ds:[arrayB]    ; Load the address of the variable arrayB into the ds:si register

    PrintArrayLoop:
        push    word ptr[si]       ; 2nd: Save the si register value on the stack
        lea     ax, ds:[fmtout]    ; 1st: Load the address of the fmtout format string into the ds:ax register
        call    PrintFormatString  ; Call the PrintFormatString procedure (ax:fmtout, si)
        add     sp, 2              ; Align the stack
        incw    si                 ; Call the incw macro. Increment si by 2
        loop    PrintArrayLoop     ; Loop while CX≠0

        xor     al, al             ; Clear the al register
        call    ExitProcess        ; Call the ExitProcess procedure
    main endp                      ; End of the main procedure

code ends                          ; End of the code segment
end main	                       ; End of file. The main procedure is the entry point

[1] [2] [2] [3] [3] [4] [5] [6] [7] [8]

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".