简体   繁体   English

在x86_64组装问题中添加双打

[英]Adding doubles in x86_64 assembly problems

Hello I am trying to learn assembly and learn how to work with floating point numbers in x86_64. 您好,我正在尝试学习汇编语言,并学习如何在x86_64中使用浮点数。 From what I understand arguments are passed in xmm0, xmm1, xmm2, and so on, and the result is returned in xmm0. 据我了解,参数在xmm0,xmm1,xmm2等中传递,结果在xmm0中返回。 So I am trying to make a simple assembly function that adds to double together. 因此,我正在尝试制作一个简单的汇编函数,以使其加倍。 Here is the function 这是功能

.text

.global floatadd
.type floatadd,@function

floatadd:
    addsd %xmm1,%xmm0
    ret

And here is the C code I am using as well. 这也是我正在使用的C代码。

#include<stdio.h>

int main(){
    double a = 1.5;
    double b = 1.2;
    double c = floatadd(a,b);
    printf("result = %f\n",c);
}

I have been trying to following what is happening in gdb. 我一直在尝试遵循gdb中发生的事情。 When I set a breakpoint in my function I can see xmm0 has 1.5 and xmm1 has 1.2 and when they are added together they 2.7. 当我在函数中设置断点时,我可以看到xmm0具有1.5,xmm1具有1.2,将它们加在一起时为2.7。 In gdb print $xmm0 gives v2_double = {2.7000000000000002, 0} However when my function returns from main and calls 在gdb打印$ xmm0给v2_double = {2.7000000000000002,0}但是当我的函数从main返回并调用

cvtsi2sd %eax,%xmm0 

Print $xmm0 becomes v2_double = {2, 0}. 打印$ xmm0变为v2_double = {2,0}。 I am not sure why gcc calls that or why it is uses the 32bit register instead of the 64bit register. 我不确定为什么gcc会调用它,或者为什么它使用32位寄存器而不是64位寄存器。 I have tried using the modifier %lf, and %f and both of them do the same thing. 我尝试使用修饰符%lf和%f,它们两者都做相同的事情。

What is happening? 怎么了?

The problem is that you failed to declare floatadd before calling it. 问题是您无法在调用前声明floatadd So the compiler assumes it returns an int in %eax and converts that int to a double. 因此,编译器假定它以%eax返回int并将该int转换为double。 Add the declaration: 添加声明:

double floatadd(double, double);

before main. 在主要之前。

Using -Wall or whatever equivalent your compiler uses to enable warnings would probably have told you about this problem... 使用-Wall或您的编译器用来启用警告的等效项可能会告诉您有关此问题的信息...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM