使用 x86 程序集计算 x*log_2(y)

Calculating x*log_2(y) with x86 assembly

所以我正在尝试让处理器计算下面的 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 进行了测试,但我总是得到纯零。我在这里做错了什么?

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