;--------------------------------------- save context

    PUSH    CORCON                      
    PUSH    W8
    PUSH    W9
    PUSH    W10

;--------------------------------------- configure accumulators and pointers    

    MOV     #0x10C0,W3                  ;config accumulators A & B for 1.31 saturation & unsigned mode
    MOV     W3,CORCON

    MOV     W0,W8                       ;move address of a1 (in W0) into pointer for X data space (W8)  
    INC2    W0,W9                       ;move address of a2 (in W0+2) into pointer for X data space (W9)
    MOV     W1,W10                      ;move address of averagedArrays (in W1) into pointer for Y data space (W10)

;--------------------------------------- calculate a1 * averagedArrays

    CLR     A,[W8],W4,[W10],W5          ;[single instruction]
                                        ;clear A, pre-load W4 with a1 (pointed to by W8)
                                        ;pre-load W5 with averagedArrays (pointed to by W10)

    MPY     W4*W5,A                     ;multiply a1 (in W4) * averagedArrays (in W5), store result in A

;--------------------------------------- calculate averagedArrays^2

    CLR     B                           ;clear B; W5 already pre-loaded with averagedArrays by earlier instruction

    MPY     W5*W5,B,[W9],W4             ;calculate square of averagedArrays (in W5), store result in B,
                                        ;preload W4 with a2 (pointed to by W9)

    SAC.R   B,W5                        ;store rounded averageArrays^2 into W5

;--------------------------------------- calculate a2*averagedArrays^2, subtract from a1*averagedArraysd

    MSC     W4*W5,A                     ;[single instruction]
                                        ;multiply a2 (in W4) * averagedArrays^2 (in W5)
                                        ;subtract result from A, store result in A

    SAC.R   A,[W2]                      ;round and store result in A to linearOut (pointed to by W2)

;---------------------------------------;restore context and return

    POP     W10
    POP     W9                         
    POP     W8
    POP     CORCON