汇编中浮点数的求和数组
Sum array of float in assembly
我在程序集 x86 中实现了一个从 C 程序调用的函数,用于添加一个浮点数数组。该函数的第一个参数是指向数组的指针,第二个参数是元素的数量。当我 运行 linux 中的代码时,出现分段错误。我做错了什么?
.text
.globl floatsum
floatsum:
push %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
movl 12(%ebp), %edx
shrl , %edx
xorps %xmm0, %xmm0
loop:
testl %edx, %edx
je end
movaps (%eax), %xmm1
addps %xmm1, %xmm0
addl , %eax
decl %edx
jmp loop
end:
# 3 2 1 0
movaps %xmm0, %xmm1 # xmm0: w z y x
# xmm1: z w x y
shufps [=10=]xb1, %xmm1, %xmm1 # 10 11 00 01 = 0xb1
addps %xmm1, %xmm0 # xmm0: w+z z+w y+x x+y
movaps %xmm0, %xmm1 # xmm1: w+z z+w y+x x+y
# xmm1: x+y y+x z+w w+z
shufps [=10=]x1b, %xmm1, %xmm1 # 00 01 10 11 = 0x1b
addps %xmm1, %xmm0 # xmm0: w+z+x+y z+w+y+x y+x+z+w x+y+w+z
#
#movd %xmm0, %eax
#pushl %eax
finst:
flds (%esp)
popl %eax
movl %ebp, %esp
popl %ebp
ret
// C代码
#include <stdio.h>
#include <stdlib.h>
float
floatsum(float *array, size_t number_of_items);
float
floatsum_c(float *array, size_t number_of_items){
float sum;
size_t i;
sum=0.0;
for(i=0; i<number_of_items;i++){
sum+=array[i];
}
return sum;
}
float *
create_array(size_t number_of_items){
float *array;
size_t i;
array=calloc(number_of_items, sizeof(float));
if(array){
for(i=0; i<number_of_items; i++){
array[i]=1.0+(float)i;
}
}
return array;
}
int
main(int argc, char **argv){
float *a;
float result;
size_t number_of_items, i;
number_of_items=8;
a=create_array(number_of_items);
if(a){
result=floatsum_c(a, number_of_items);
printf("Sum (c version): %f\n", result);
result=floatsum(a, number_of_items);
printf("Sum (asm version): %f\n", result);
free(a);
}
return 0;
}
正如 Paul 所提到的,这可能是一个对齐问题。从您的 C 代码可以清楚地看出,您的浮点数组不能保证在 16 字节边界上对齐。失败是这一行:
movaps (%eax), %xmm1
原因是MOVAPS有这个需求:
When the source or destination operand is a memory operand, the operand must be aligned on a 16-byte (128-bit version) or 32-byte (VEX.256 encoded version) boundary or a general-protection exception (#GP) will be generated.
由于您使用的是 128 位向量寄存器,因此需要 16 字节对齐。您有两个选择:
- 将MOVAPS更改为MOVUPS以便可以进行未对齐的内存访问
- 修改您的 C 代码以创建在 16 字节边界上对齐的浮点数组
第一个解决方案需要:
movaps (%eax), %xmm1
改成;
movups (%eax), %xmm1
第二种解决方案是避免使用calloc and utilize a function that allows you to create objects with 16-byte alignment. If using C11 then you can use the function aligned_alloc and memset 将数组归零。您的 create_array
可能看起来像:
float *
create_array(size_t number_of_items)
{
float *array = NULL;
size_t i;
array=(float *)aligned_alloc(16, number_of_items * sizeof(*array));
if(array){
memset (array, 0x00, number_of_items * sizeof(*array));
for(i=0; i<number_of_items; i++){
array[i]=1.0+(float)i;
}
}
return array;
}
如果您不使用 C11,则可以使用 POSIX 函数 posix_memalign 和 memset on Linux。代码可能类似于:
float *
create_array(size_t number_of_items)
{
float *array = NULL;
size_t i;
if (!posix_memalign((void **)&array, 16, number_of_items * sizeof(*array))){
memset (array, 0x00, number_of_items * sizeof(*array));
for(i=0; i<number_of_items; i++){
array[i]=1.0+(float)i;
}
}
return array;
}
您还必须取消注释这些行:
#movd %xmm0, %eax
#pushl %eax
这样它们就会这样显示:
movd %xmm0, %eax
pushl %eax
注意:尽管我使用memset 将浮点数组置零,就像calloc 一样有,你的代码实际上并不需要它,因为你之后将所有元素初始化为特定值。在您的情况下,可以删除对 memset 的调用。
我在程序集 x86 中实现了一个从 C 程序调用的函数,用于添加一个浮点数数组。该函数的第一个参数是指向数组的指针,第二个参数是元素的数量。当我 运行 linux 中的代码时,出现分段错误。我做错了什么?
.text
.globl floatsum
floatsum:
push %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
movl 12(%ebp), %edx
shrl , %edx
xorps %xmm0, %xmm0
loop:
testl %edx, %edx
je end
movaps (%eax), %xmm1
addps %xmm1, %xmm0
addl , %eax
decl %edx
jmp loop
end:
# 3 2 1 0
movaps %xmm0, %xmm1 # xmm0: w z y x
# xmm1: z w x y
shufps [=10=]xb1, %xmm1, %xmm1 # 10 11 00 01 = 0xb1
addps %xmm1, %xmm0 # xmm0: w+z z+w y+x x+y
movaps %xmm0, %xmm1 # xmm1: w+z z+w y+x x+y
# xmm1: x+y y+x z+w w+z
shufps [=10=]x1b, %xmm1, %xmm1 # 00 01 10 11 = 0x1b
addps %xmm1, %xmm0 # xmm0: w+z+x+y z+w+y+x y+x+z+w x+y+w+z
#
#movd %xmm0, %eax
#pushl %eax
finst:
flds (%esp)
popl %eax
movl %ebp, %esp
popl %ebp
ret
// C代码
#include <stdio.h>
#include <stdlib.h>
float
floatsum(float *array, size_t number_of_items);
float
floatsum_c(float *array, size_t number_of_items){
float sum;
size_t i;
sum=0.0;
for(i=0; i<number_of_items;i++){
sum+=array[i];
}
return sum;
}
float *
create_array(size_t number_of_items){
float *array;
size_t i;
array=calloc(number_of_items, sizeof(float));
if(array){
for(i=0; i<number_of_items; i++){
array[i]=1.0+(float)i;
}
}
return array;
}
int
main(int argc, char **argv){
float *a;
float result;
size_t number_of_items, i;
number_of_items=8;
a=create_array(number_of_items);
if(a){
result=floatsum_c(a, number_of_items);
printf("Sum (c version): %f\n", result);
result=floatsum(a, number_of_items);
printf("Sum (asm version): %f\n", result);
free(a);
}
return 0;
}
正如 Paul 所提到的,这可能是一个对齐问题。从您的 C 代码可以清楚地看出,您的浮点数组不能保证在 16 字节边界上对齐。失败是这一行:
movaps (%eax), %xmm1
原因是MOVAPS有这个需求:
When the source or destination operand is a memory operand, the operand must be aligned on a 16-byte (128-bit version) or 32-byte (VEX.256 encoded version) boundary or a general-protection exception (#GP) will be generated.
由于您使用的是 128 位向量寄存器,因此需要 16 字节对齐。您有两个选择:
- 将MOVAPS更改为MOVUPS以便可以进行未对齐的内存访问
- 修改您的 C 代码以创建在 16 字节边界上对齐的浮点数组
第一个解决方案需要:
movaps (%eax), %xmm1
改成;
movups (%eax), %xmm1
第二种解决方案是避免使用calloc and utilize a function that allows you to create objects with 16-byte alignment. If using C11 then you can use the function aligned_alloc and memset 将数组归零。您的 create_array
可能看起来像:
float *
create_array(size_t number_of_items)
{
float *array = NULL;
size_t i;
array=(float *)aligned_alloc(16, number_of_items * sizeof(*array));
if(array){
memset (array, 0x00, number_of_items * sizeof(*array));
for(i=0; i<number_of_items; i++){
array[i]=1.0+(float)i;
}
}
return array;
}
如果您不使用 C11,则可以使用 POSIX 函数 posix_memalign 和 memset on Linux。代码可能类似于:
float *
create_array(size_t number_of_items)
{
float *array = NULL;
size_t i;
if (!posix_memalign((void **)&array, 16, number_of_items * sizeof(*array))){
memset (array, 0x00, number_of_items * sizeof(*array));
for(i=0; i<number_of_items; i++){
array[i]=1.0+(float)i;
}
}
return array;
}
您还必须取消注释这些行:
#movd %xmm0, %eax
#pushl %eax
这样它们就会这样显示:
movd %xmm0, %eax
pushl %eax
注意:尽管我使用memset 将浮点数组置零,就像calloc 一样有,你的代码实际上并不需要它,因为你之后将所有元素初始化为特定值。在您的情况下,可以删除对 memset 的调用。