ArrayCopyEx function

The ArrayCopyEx function copies elements from the first array into the second one. The pointers to the arrays and the number of elements are passed through the stack. The function automatically aligns the stack to 6 bytes.

Parameters (stack): 1. Pointer to the source array. 2. Pointer to the destination array. 3. Number of elements.

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 ArrayCopyEx function to copy an array.

title Author: Evgeny Sergeevich Dovgopolov

.286C
.model small
.stack 100h

data segment public
    array1 dw  23, 54, 83, 91
    arrlen equ ($-array1) / 2
    array2 dw  arrlen dup(?)
    output db  "Copied array: \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:[array2]        ; 2nd: Push the address of the array array2 onto the top of the stack
        pushs   ds:[array1]        ; 1st: Push the address of the array array1 onto the top of the stack
        call    ArrayCopyEx        ; Call the ArrayCopyEx(array1, array2, arrlen) procedure

        pushs   ds:[output]        ; Push the address of the output variable onto the top of the stack
        call    WriteStringEx      ; Call the WriteStringEx(output) procedure

        loopcnt 4                  ; Set the counter to 4. CX := 4
        leabx   ds:[array2]        ; Load the address of the array array2 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
        incw    si                 ; Increase the index si by 2
        loop    PrintArrayLoop     ; Loop while the cx register is not equal to 0
        add     sp, 8              ; 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

Copied array: [23] [54] [83] [91]

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