Skip to main content
Improved Formatting; Fixed grammar
Source Link
Quill
  • 11.9k
  • 5
  • 40
  • 93

Harmonic mean is definedefined as:

I would like to know if:

  • My code follows common practices.
  •  
  •  
  • My code is formatted correctly.
  • I
  • If comments need to improve commentsbe improved.
  •  
  •  
  • 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.
  

Harmonic mean is define as:

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.
 

Harmonic mean is defined as:

I would like to know if:

  • My code follows common practices. 
  • My code is formatted correctly.
  • If comments need to be improved. 
  • 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. 
Notice removed Reward existing answer by 200_success
Bounty Ended with Edward's answer chosen by 200_success
Notice added Reward existing answer by 200_success
Bounty Started worth 150 reputation by 200_success
Tweeted twitter.com/#!/StackCodeReview/status/540526686602027008
added 28 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Code Improvements? Calculating the harmonic average

Harmonic mean is define as: \$ \frac{n}{(1/x_1) + (1/x_2) + ... + (1/x_n)} \$ where

$$ \frac{n}{\dfrac{1}{x_1} + \dfrac{1}{x_2} + \cdots + \dfrac{1}{x_n}}$$

where \$n = \$ number of inputs and \$x_i = \$ a real number.

Code Improvements?

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.

Calculating the harmonic average

Harmonic mean is define as:

$$ \frac{n}{\dfrac{1}{x_1} + \dfrac{1}{x_2} + \cdots + \dfrac{1}{x_n}}$$

where \$n = \$ number of inputs and \$x_i = \$ a real number.

Source Link

Code Improvements?

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