ArrayMergeSortEx function
The ArrayMergeSortEx function merges two arrays into a third one and sorts it. The parameters are passed through the stack. It returns the number of elements in CX. The function automatically aligns the stack to 12 bytes.
Parameters (stack): 1. Pointer to the first array. 2. Pointer to the second array. 3. Pointer to the third array. 4. Length of the first array. 5. Length of the second array. 6. Sorting mode (ASCENDING/DESCENDING).
Return value: CX - the number of elements in the third array.
Flag state: The function does not modify the flags.
Program source code
Intel 8086 assembly program example demonstrating the use of the ArrayMergeSortEx function to merge two arrays and sort them in descending order.
title .286C .model small .stack 100h data segment public arrayA dw 4, 5, 2, 3 arrAlen equ ($-arrayA) / 2 arrayB dw 2, 6, 3, 1, 7, 8 arrBlen equ ($-arrayB) / 2 arrayC dw arrAlen + arrBlen dup(0) fmtout db "[%d] \0" data ends code segment public assume ds:data, cs:code include Handyfar.inc ; Include the file with functions include Handymac.inc ; Include the file with macros main proc call SetDataSegment ; Call the SetDataSegment procedure push DESCENDING ; 6th: Sort in descending order push arrBlen ; 5th: Push the length of the array arrayB onto the top of the stack push arrAlen ; 4th: Push the length of the array arrayA onto the top of the stack pushs ds:[arrayC] ; 3rd: Push the address of the array arrayC onto the top of the stack pushs es:[arrayB] ; 2nd: Push the address of the array arrayB onto the top of the stack pushs ds:[arrayA] ; 1st: Push the address of the array arrayA onto the top of the stack call ArrayMergeSortEx ; Call the ArrayMergeSortEx(aA, aB, aC, 4, 6, 1) procedure := CX leabx ds:[arrayC] ; Load the address of the array arrayC into the ds:bx register xor si, si ; Clear the index register PrintArrayLoop: push word ptr[bx+si] ; 2nd: Push the value in bx at index si onto the top of the stack leaax ds:[fmtout] ; 1st: Load the address of the fmtout format string into the ds:ax register call PrintFormatString ; Call the PrintFormatString(ax:fmtout, bx) procedure add sp, 2 ; Align the stack incw si ; Increase the index si by 2 loop PrintArrayLoop ; Loop while the cx register is not equal to 0 push 0 ; Push the byte return code onto the top of the stack call ExitProcessEx ; Call the ExitProcessEx(0) procedure main endp code ends end main
[8] [7] [6] [5] [4] [3] [3] [2] [2] [1]
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".