gpt4 book ai didi

c - 如何在汇编中使用 float ?

转载 作者:行者123 更新时间:2023-12-05 03:12:27 24 4
gpt4 key购买 nike

我需要从 C 发送两个实数(使用 extern 'C' double (double num1,double num2))到返回加法的程序集过程这些数字。我还不知道该怎么做。

我试过了,但没用。

.data

res dq 0

.code

procSuma proc num1,num2

fld num1 ; load num1 and push it onto the fpu stack
fld num2 ; load num2 and push it onto the fpu stack
faddp ; pop two numbers, add them, push sum on the stack
fstp res ; pop sum from the stack and store it in res

procSuma endp
end

最佳答案

您出错的地方是您将最初不包含在函数中的粘贴代码复制到函数中,我相信您是从这里得到的。 Adding floating point/double numbers in assembly

由于汇编代码现在包含在 proc 中,因此不再需要变量来存储结果,因为结果存储在 st(0) 地址的堆栈中,这将成为你的返回值(value)。当调用 fstp 时,它会弹出堆栈(提示:末尾的 p),因此存储在 st(0) 的返回值不再有效。删除 fstp 指令将为您提供预期的结果。

这就是代码正在做的事情。

procSuma proc num1,num2   

fld num1 ; This pushes num1 onto the stack at the address of st(0)
fld num2 ; This pushes num2 onto the stack at the address of st(1)
faddp ; This multiplies st(1) and st(0), stores the result at st(0), and then pops the variable stored at st(1) off the stack
fstp res ; !REMOVE THIS. This is why you aren't getting the expected result.It pops the variable in st(0) off the stack, which is your return value.

procSuma endp

关于c - 如何在汇编中使用 float ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33716006/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com