作为一项家庭作业,我们需要使用由C程序驱动的汇编程序计算调和平均值。谐波平均收益率不正确结果
我们使用的是64位linux机器,并且需要使用64位浮点数。
我是Assembly的新手。对于任何不好的编码做法,或者我的代码是错误的,我表示歉意。
代码的问题是结果只返回以浮点格式输入的最后一个数字。我不知道错误发生在哪里,但我相信它在addDen
函数中。
作为示例:如果要输入数字5,6,7,8,结果将返回8.0000。
这里是我的汇编程序代码:
;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
为了重建逻辑错误,我也包括我的驱动程序:
/*
* Harmonic Mean Driver
* Tyler Weaver
* 03-12-2014
*/
#include<stdio.h>
#define ARRAYSIZE 4
double harmonicMean(double *, unsigned);
int main(int argc, char **argv) {
int i;
double ary[ARRAYSIZE];
double hm;
printf("Enter %d f.p. values: ", ARRAYSIZE);
for (i = 0; i < ARRAYSIZE; i++) {
scanf(" %lf", &ary[i]);
}
hm = harmonicMean(ary, ARRAYSIZE);
printf("asm: harmonic mean is %lf\n", hm);
return 0;
}
任何帮助将是非常感激!
“double *”和“unsigned”的大小与“RDI”和“RSI”的大小相同吗?建议'printf(“%zu%zu \ n”,sizeof(double *),sizeof(unsigned));'验证。 – chux 2014-12-03 22:55:46
它们应该分别为4个字节。我们被告知使用double和unsigned为驱动程序。在分配细节。我是Assembly的新手,所以我不知道这是否溢出寄存器。该数组将会更长,但RDI寄存器应该只保存阵列的前4个字节。 – 2014-12-03 22:59:40
同意_should_每个都是4个字节。 C代码是否用'printf(“%zu%zu \ n”,sizeof(double *),sizeof(unsigned))来报告;'? – chux 2014-12-03 23:03:23