gpt4 book ai didi

assembly - NASM float -操作码和操作数的无效组合

转载 作者:行者123 更新时间:2023-12-02 10:43:59 25 4
gpt4 key购买 nike

我正在尝试从this article on x86 assembly floating point编译以下代码示例(NASM语法):

;; c^2 = a^2 + b^2 - cos(C)*2*a*b
;; C is stored in ang

global _start

section .data
a: dq 4.56 ;length of side a
b: dq 7.89 ;length of side b
ang: dq 1.5 ;opposite angle to side c (around 85.94 degrees)

section .bss
c: resq 1 ;the result ‒ length of side c

section .text
_start:

fld qword [a] ;load a into st0
fmul st0, st0 ;st0 = a * a = a^2

fld qword [b] ;load b into st1
fmul st1, st1 ;st1 = b * b = b^2

fadd st1, st0 ;st1 = a^2 + b^2

fld qword [ang] ;load angle into st0
fcos ;st0 = cos(ang)

fmul qword [a] ;st0 = cos(ang) * a
fmul qword [b] ;st0 = cos(ang) * a * b
fadd st0, st0 ;st0 = cos(ang) * a * b + cos(ang) * a * b = 2(cos(ang) * a * b)

fsubp st1, st0 ;st1 = st1 - st0 = (a^2 + b^2) - (2 * a * b * cos(ang))
;and pop st0

fsqrt ;take square root of st0 = c

fst qword [c] ;store st0 in c ‒ and we're done!

当我执行以下命令时:
nasm -f elf32 cosineSample.s -o cosineSample.o

对于 fmul st1, st1行,出现以下错误:
error: invalid combination of opcode and operands

我需要怎么做才能解决这个问题?我需要将特殊参数传递给 nasm吗?代码示例是否错误?

最佳答案

不幸的是,该代码已损坏。 fmul无法对st1, st1进行操作,但是即使这样做,它也无法执行作者想要的操作。根据评论,他想计算b*b,但b当时在st0中。注释load b into st1是错误的,fld始终加载到st0(堆栈的顶部)中。您需要将fmul st1, st1更改为fmul st0, st0。此外,为了获得正确的结果,以下fadd st1, st0也必须反转。该代码还会使fpu堆栈变脏。

还要注意,该程序没有结尾,因此除非您添加显式的exit系统调用,否则它将出现段错误。

这是固定的代码,转换为gnu汇编器语法:

.intel_syntax noprefix

.global _start

.data
a: .double 4.56 # length of side a
b: .double 7.89 # length of side b
ang: .double 1.5 # opposite angle to side c (around 85.94 degrees)

.lcomm c, 8

.text
_start:

fld qword ptr [a] # load a into st0
fmul st # st0 = a * a = a^2

fld qword ptr [b] # load b into st0
fmul st # st0 = b * b = b^2

faddp # st0 = a^2 + b^2

fld qword ptr [ang] # load angle into st0
fcos # st0 = cos(ang)

fmul qword ptr [a] # st0 = cos(ang) * a
fmul qword ptr [b] # st0 = cos(ang) * a * b
fadd st # st0 = cos(ang) * a * b + cos(ang) * a * b = 2(cos(ang) * a * b)

fsubp # st1 = st1 - st0 = (a^2 + b^2) - (2 * a * b * cos(ang))
# and pop st0

fsqrt # take square root of st0 = c

fstp qword ptr [c] # store st0 in c - and we're done!

# end program
mov eax, 1
xor ebx, ebx
int 0x80

关于assembly - NASM float -操作码和操作数的无效组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54487573/

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