The ArrayCopyIf function

The ArrayCopyIf function copies elements from the first array to the second array based on a condition specified by a user function passed through a pointer.

Parameters: DS:AX - source array; ES:BX - destination array; CX - pointer to the condition function; DX - size of the source array.

Return value: CX - number of copied elements.

Flag state: The function does not modify the flags.

Program source code

An example program for the Intel 8086 assembler demonstrating the use of the ArrayCopyIf function to copy array elements by condition.

title Author: Evgeny Sergeevich Dovgopolov

.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                ; Start of the code segment
    assume  ds:data, cs:code
    include Handyfar.inc           ; Include the file with functions
    include Handymac.inc           ; Include the file with macros

    main proc                      ; Start of the main procedure
        call    SetDataSegment     ; Call the SetDataSegment procedure

        mov     dx, arrlen         ; Copy the array length into the dx register
        lea     cx, IsDivBy2       ; Load the address of the procedure IsDivBy2 into the cx register
        lea     bx, es:[buffer]    ; Load the address of the variable buffer into the es:bx register
        lea     ax, ds:[array]     ; Load the address of the variable array into the ds:ax register
        call    ArrayCopyIf        ; Call the ArrayCopyIf procedure (ds:ax, es:bx, cx, dx) := CX

        lea     bx, ds:[buffer]    ; Load the address of the array buffer into the ds:bx register
        xor     si, si             ; Clear the si index register

    PrintArrayLoop:
        push    word ptr[bx+si]    ; 2nd: Save the value at bx with index si on the stack
        lea     ax, ds:[fmtout]    ; 1st: Load the address of the fmtout format string into the ds:ax register
        call    PrintFormatString  ; Call the PrintFormatString procedure (ax:fmtout, bx)
        add     sp, 2              ; Align the stack
        incw    si                 ; Increment the si index by 2
        loop    PrintArrayLoop     ; Loop while the cx register is not equal to 0

        xor     al, al             ; Clear the al register
        call    ExitProcess        ; Call the ExitProcess procedure
    main endp                      ; End of the main procedure

    IsDivBy2 proc                  ; Start of the IsDivBy2 procedure
        push    ax                 ; Always save the ax register value on the stack
        push    bx                 ; Always save the bx register value on the stack
        push    dx                 ; Always save the dx register value 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                 ; Always pop the value from the stack back into the dx register
        pop     bx                 ; Always pop the value from the stack back into the bx register
        pop     ax                 ; Always pop the value from the stack back into the ax register
        ret                        ; Return from the function
    IsDivBy2 endp                  ; End of the IsDivBy2 procedure

code ends                          ; End of the code segment
end main	                       ; End of file. The main procedure is the entry point

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

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