;macro4.asm
;4 arguments: <list>, <length>, <average>

;---------------------------------------------------------------------
%macro aver 4           ;array, length, average, average squared
mov eax, 0
mov ecx, dword[%2]      ;second argument: length
mov r12, 0
lea rbx, [%1]           ;load effective address of 1st element of array

%%sumLoop:
add eax, dword[rbx+r12*4]
inc r12  
; cmp r12, ecx #error , unnecessary, ecx=0
; jl           #error ; ecx=0
loop %%sumLoop

cdq                       ;convert dword in eax in qword in edx:eax
idiv dword[%2]            ;eax = eax/<src>, remainder in edx
mov dword[%3], eax        ;average
mov r13d, dword[%3]       ;average
mov dword[%4], eax
imul dword[%4]            ;edx:eax=eax*src, average squared

%endmacro
;-----------------------------------------------------------------------

section .data
EXIT_SUCCESS equ 0
SYS_exit equ 60
list1 dd 4, 5, 2, -3, 1
len1 dd 5
ave1 dd 0
avesq1 dd 0
list2 dd 2, 6, 3, -2, 1, 8, 19
len2 dd 7
ave2 dd 0
avesq2 dd 0
list3 dd 44
len3 dd 1
ave3 dd 0
avesq3 dd 0

section .text
global _start
_start:

aver list1, len1, ave1, avesq1
aver list2, len2, ave2, avesq2
aver list3, len3, ave3, avesq3

mov rax, SYS_exit
mov rdi, EXIT_SUCCESS
syscall

