; callee00.asm
;
; ascending sorted values array
;
; arr, address – rdi
; len, dword value – esi
; lower median, address - rcx
; upper median, address - r8
; min, address – rdx
; max, address - r9
; sum, address – stack (rbp+16)
; ave, address – stack (rbp+24)
;
section .data
section .text
global callee00
callee00:
push rbp            ;prologue
mov rbp, rsp
push rbx
push r12
mov eax, dword[rdi]       ; get min
mov dword[rdx], eax        ; return min 
mov r12, rsi              ; get len 
dec r12                   ; len-1
mov eax, dword[rdi+r12*4] ; get max
mov dword[r9], eax        ; return max
; get medians
mov rax, rsi
mov rdx, 0
mov r12, 2
div r12    ; unsigned rax=length/2, remainder in rdx
cmp rdx, 0    ; length = even or odd ?
je evenLength ; remainder = 0 = even length
;
mov r12d, dword[rdi+rax*4]  ; get arr[length/2]
mov dword[rcx], r12d        ; return median
jmp medDone
evenLength:
mov r12d, dword[rdi+rax*4]  ; get arr[length/2]
mov dword[rcx], r12d       ; return upper median
dec rax
mov r12d, dword[rdi+rax*4]  ; get arr[length/2-1]
mov dword[r9], r12d        ; get lower median
;
medDone:
mov rax, 0
mov r12, 0
;
sumLoop:
add eax, dword[rdi+r12*4]
inc r12
cmp r12, rsi
jl sumLoop
;
mov rbx, qword[rbp+16]   ; get sum address
mov dword[rbx], eax      ; return sum 
;
; average
;
cdq   ;convert doubleword eax in quadword edx:eax
idiv rsi ; signed rax=rax/rsi, rem.in rdx, sum/len
mov r12, qword[rbp+24]  ; get ave address
mov dword[r12], eax     ; return average
;
pop r12
pop rbx
pop rbp
ret

 

