ArrayCopyIfNotEx 函数

ArrayCopyIfNotEx 函数在元素不满足用户函数条件时,将其从第一个数组复制到第二个数组。参数通过栈传递。函数会自动将栈对齐 8 个字节。

参数(栈): 1. 源数组的指针。2. 目标数组的指针。3. 条件函数的指针。4. 原数组的大小。

返回值: CX — 已复制元素的数量。

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

程序源代码

Intel 8086 汇编程序示例,演示使用 ArrayCopyIfNotEx 函数复制不满足条件的元素。

title Author: Dovgopolov Evgeny Sergeevich

.286C
.model small
.stack 100h

data segment public
    array  dw  1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12
    arrlen equ ($-array) / 2
    buffer dw  arrlen dup(?)
    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             ; 第4步:将数组 array 的长度压入栈顶
        pushs   IsDivBy2           ; 第3步:将函数 IsDivBy2 的地址压入栈顶
        pushs   es:[buffer]        ; 第2步:将数组 buffer 的地址压入栈顶
        pushs   ds:[array]         ; 第1步:将数组 array 的地址压入栈顶
        call    ArrayCopyIfNotEx   ; 调用过程 ArrayCopyIfNotEx(ar, buf, IsDivBy2, 12) := CX

        leabx   ds:[buffer]        ; 将数组 buffer 的地址装入寄存器 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              ; 对齐栈
        incw    si                 ; 将索引 si 增加 2
        loop    PrintArrayLoop     ; 当寄存器 cx 不为 0 时执行循环

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

    IsDivBy2 proc                  ; IsDivBy2 过程开始
        push    ax                 ; 必须将寄存器 ax 的值保存到栈中
        push    bx                 ; 必须将寄存器 bx 的值保存到栈中
        push    dx                 ; 必须将寄存器 dx 的值保存到栈中

        mov     bx, 2              ; 将数值 2 复制到寄存器 bx
        cwd                        ; 对寄存器 dx 执行符号扩展
        idiv    bx                 ; 将寄存器 ax 中的值除以 bx
        test    dl, dl             ; 检查该值是否为 0

        pop     dx                 ; 必须将栈中的值弹出回寄存器 dx
        pop     bx                 ; 必须将栈中的值弹出回寄存器 bx
        pop     ax                 ; 必须将栈中的值弹出回寄存器 ax
        ret                        ; 从函数返回
    IsDivBy2 endp                  ; IsDivBy2 过程结束

code ends
end main

[1] [3] [5] [7] [9] [11]

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