In this program I was required to calculate the Harmonic Average using an assembly program with a C driver.
Harmonic mean is define as: \$ \frac{n}{(1/x_1) + (1/x_2) + ... + (1/x_n)} \$
where \$n = \$ number of inputs and \$x_i = \$ a real number.
I would like to know if
- My code follows common practices.
- My code is formatted correctly.
- I need to improve comments.
- My code is straightforward; that is, I have achieved the goal with the least amount of work.
- There any improvements I should make in my code.
Attached below is my program in Assembly:
;Assembly function that computs the harmonic mean
;of an array of 64-bit floating-point numbers.
;Retrieves input using a C program.
;
;Harmonic mean is defined as Sum(n/((1/x1) + (1/x2) + ... + (1/xn)))
;
; expects:
; RDI - address of array
; RSI - length of the array
; returns
; XMMO - the harmonic average of array's values
global harmonicMean
section .data
Zero dd 0.0
One dd 1.0
section .text
harmonicMean:
push rbp
mov rbp, rsp ;C prologue
movss xmm10, [Zero] ;Holds tally of denominator
cvtsi2ss xmm0, rsi ;Take length and put it into xmm0 register
.whileLoop:
cmp rsi, 0 ;Is the length of array 0?
je .endwhile
call addDen ;Compute a denominator value and add it to sum
add rdi, 4 ;Add size of float to address
dec rsi ;Decrease the length
jmp .whileLoop
.endwhile:
divss xmm0, xmm10
leave
ret
;Calculates a number in the denominator
addDen:
push rdi
movss xmm8, [One]
movss xmm9, [rdi]
divss xmm8, xmm9
addss xmm10, xmm8
pop rdi
ret