【问题标题】:Calculating x*log_2(y) with x86 assembly使用 x86 程序集计算 x*log_2(y)
【发布时间】:2020-06-06 18:32:14
【问题描述】:

所以我正在尝试让处理器计算下面的x*log_2(y) 是我当前的汇编代码(nasm 语法)。我在XMM0 寄存器中有 y 的输入值,X 将是 10 的常数。

    MOVSD QWORD [RSP], XMM0 ;Copy result of prev calculation to the stack
    FLD QWORD [RSP] ;Load the float from XMM0 to the fpu stack

    ;Load number 10 to the fpu stack
    MOV RAX, 10
    PUSH RAX
    FLD QWORD [RSP] ;ST(0)
    POP RAX
    ;Do the math y = xmm0, x=10 X*LOG2(Y)
    FYL2X ;ST(1) = OUT
    FSTP ;just remove ST(0), no need to preserve
    FSTP QWORD [RSP];Pop the result from the logarithm to the stack

    MOV RDI, QWORD [RSP]

    call printfcallfloat

我在 XMM0 寄存器中使用 2.0 的值进行了测试,但我总是得到纯零。我在这里做错了什么?

【问题讨论】:

  • 您确定 printfcallfloat 需要 RDI 中的浮点参数,而不是 XMM0 中的浮点参数吗?
  • @prl 是的,这是我自己编写函数时的情况

标签: assembly floating-point x86-64 logarithm


【解决方案1】:

FYL2X 大放异彩。之后就不需要 FSTP。
操作数也被反转。它需要先推10。
要加载整数,请使用 FILD,而不是 FLD。

;Load number 10 to the fpu stack
PUSH 10
FILD QWORD [RSP]

MOVSD QWORD [RSP], XMM0 ;Copy result of prev calculation to the stack
FLD QWORD [RSP] ;Load the float from XMM0 to the fpu stack

;Do the math x = xmm0, y = 10; y * LOG2(x)
FYL2X
FSTP QWORD [RSP];Pop the result from the logarithm to the stack

POP RDI
call printfcallfloat

【讨论】:

  • 由于某种原因无法正常工作,将更新我的问题
  • 没关系,这是出于其他原因,您的答案是正确的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-29
  • 1970-01-01
  • 2020-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多