ArrayCopyIfEx function

The ArrayCopyIfEx function copies elements from the first array into the second one if they satisfy the condition of a user-defined function. The parameters are passed through the stack. The function automatically aligns the stack to 8 bytes.

Parameters (stack): 1. Pointer to the source array. 2. Pointer to the destination array. 3. Pointer to the condition function. 4. Size of the source array.

Return value: CX - the number of copied elements.

Flag state: The function does not modify the flags.

Program source code

Intel 8086 assembly program example demonstrating the use of the ArrayCopyIfEx function for selective copying of array elements by means of a user-defined condition function.

title Author: Evgeny Sergeevich Dovgopolov

.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 the file with functions
    include Handymac.inc           ; Include the file with macros

    main proc
        call    SetDataSegment     ; Call the SetDataSegment procedure

        push    arrlen             ; 4th: Push the length of the array array onto the top of the stack
        pushs   IsDivBy2           ; 3rd: Push the address of the IsDivBy2 function onto the top of the stack
        pushs   es:[buffer]        ; 2nd: Push the address of the array buffer onto the top of the stack
        pushs   ds:[array]         ; 1st: Push the address of the array array onto the top of the stack
        call    ArrayCopyIfEx      ; Call the ArrayCopyIfEx(arr, buf, IsDivBy2, 12) procedure := CX

        leabx   ds:[buffer]        ; Load the address of the array buffer 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

    IsDivBy2 proc                  ; Start of the IsDivBy2 procedure
        push    ax                 ; The value of the ax register must be saved on the stack
        push    bx                 ; The value of the bx register must be saved on the stack
        push    dx                 ; The value of the dx register must be saved on the stack

        mov     bx, 2              ; Copy the number 2 into the bx register
        cwd                        ; Perform sign extension of the dx register
        idiv    bx                 ; Divide the value in the ax register by bx
        test    dl, dl             ; Test the value for 0

        pop     dx                 ; The value must be popped from the stack back into the dx register
        pop     bx                 ; The value must be popped from the stack back into the bx register
        pop     ax                 ; The value must be popped from the stack back into the ax register
        ret                        ; Return from the function
    IsDivBy2 endp                  ; End of the IsDivBy2 procedure

code ends
end main

[2] [4] [6] [8] [10] [12]

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