ArraySortEx function

The ArraySortEx function sorts an array in ascending (ASCENDING) or descending (DESCENDING) order. The parameters are passed through the stack. The function automatically aligns the stack to 6 bytes.

Parameters (stack): 1. Pointer to the array. 2. Length of the array. 3. Sorting mode (ASCENDING or DESCENDING).

Return value: The function does not return a value.

Flag state: The function does not modify the flags.

Program source code

Intel 8086 assembly program example demonstrating the use of the ArraySortEx function to sort an array in descending order.

title Author: Evgeny Sergeevich Dovgopolov

.286C
.model small
.stack 100h

data segment public
    array  dw  5, 5, 2, 6, 5, 10, 4, 2, 10, 0, 0, 1, 8, 2, 3, 9, 0, 7
    arrlen equ ($-array) / 2
    arrbuf dw  arrlen 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    arrlen             ; 3rd: Push the length of the array onto the top of the stack
        pushs   es:[arrbuf]        ; 2nd: Push the length of the array arrbuf onto the top of the stack
        pushs   ds:[array]         ; 1st: Push the address of the array array onto the top of the stack
        call    ArrayMakeUniqueEx  ; Call the ArrayMakeUniqueEx(array, unique, 18) procedure := CX

        push    DESCENDING         ; 3rd: Sort in descending order
        push    cx                 ; 2nd: Push the length of the array onto the top of the stack
        pushs   ds:[arrbuf]        ; 1st: Push the length of the array arrbuf onto the top of the stack
        call    ArraySortEx        ; Call the ArraySortEx(arrbuf, cx, DESCENDING) procedure

        leabx   ds:[arrbuf]        ; Load the address of the array arrbuf 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              ; Increase the index si by 2
        incw    si                 ; Loop while the cx register is not equal to 0
        loop    PrintArrayLoop     ; Align the stack

        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

[10] [9] [8] [7] [6] [5] [4] [3] [2] [1] [0]

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