ArraySortEx 函数

ArraySortEx 函数按升序(ASCENDING)或降序(DESCENDING)对数组排序。参数通过栈传递。函数会自动将栈对齐 6 个字节。

参数(栈): 1. 数组指针。2. 数组长度。3. 排序模式(ASCENDING 或 DESCENDING)。

返回值: 函数不返回值。

标志状态: 函数不改变标志状态。

程序源代码

Intel 8086 汇编程序示例,演示使用 ArraySortEx 函数按降序对数组排序。

title Author: Dovgopolov Evgeny Sergeevich

.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 Handymac.inc           ; 包含宏文件

    main proc
        call    SetDataSegment     ; 调用过程 SetDataSegment

        push    arrlen             ; 第3步:将数组长度压入栈顶
        pushs   es:[arrbuf]        ; 第2步:将数组 arrbuf 的长度压入栈顶
        pushs   ds:[array]         ; 第1步:将数组 array 的地址压入栈顶
        call    ArrayMakeUniqueEx  ; 调用过程 ArrayMakeUniqueEx(array, unique, 18) := CX

        push    DESCENDING         ; 第3步:按降序排序
        push    cx                 ; 第2步:将数组长度压入栈顶
        pushs   ds:[arrbuf]        ; 第1步:将数组 arrbuf 的长度压入栈顶
        call    ArraySortEx        ; 调用过程 ArraySortEx(arrbuf, cx, DESCENDING)

        leabx   ds:[arrbuf]        ; 将数组 arrbuf 的地址装入寄存器 ds:bx
        xor     si, si             ; 将索引寄存器清零

    PrintArrayLoop:
        push    word ptr[bx+si]    ; 第2步:将 bx 中索引 si 处的值压入栈顶
        leaax   ds:[fmtout]        ; 第1步:将格式字符串 fmtout 的地址装入寄存器 ds:ax
        call    PrintFormatString  ; 调用过程 PrintFormatString(ax:fmtout, bx)
        add     sp, 2              ; 将索引 si 增加 2
        incw    si                 ; 当寄存器 cx 不为 0 时执行循环
        loop    PrintArrayLoop     ; 对齐栈

        push    0                  ; 将字节值(返回代码)压入栈顶
        call    ExitProcessEx      ; 调用过程 ExitProcessEx(0)
    main endp

code ends
end main

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

示例取自教材 "HANDYFAR - MASM (Dovgopolov Evgeny Sergeevich)".