The ArrayGetAverage function

The ArrayGetAverage function calculates the average value of the numbers in a given array.

Parameters: AX: pointer to the array; BX: array length.

Return value: AX: average value of the numbers in the array.

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 ArrayGetAverage function to calculate the arithmetic mean of an array.

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    array  dw  6, 11, 7
    arrlen equ ($-array) / 2
    fmtout db  "Average number: %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

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

        mov     bx, arrlen         ; Copy the array length into the bx register
        lea     ax, ds:[array]     ; Load the address of the variable array into the ds:ax register
        call    ArrayGetAverage    ; Call the ArrayGetAverage procedure (ds:ax, bx) := AX

        push    ax                 ; 2nd: save the ax register value 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, ax)
        add     sp, 2              ; Align the stack

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

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

Average number: 8

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