天天看点

第67部分- Linux x86 64位汇编 FPU之三角函数第67部分- Linux x86 64位汇编 FPU之三角函数正弦余弦示例

第67部分- Linux x86 64位汇编 FPU之三角函数

一般的三角函数,比如正弦,余弦和正切等都可以轻松的通过FPU来计算得到。

正弦的指令是fsin,余弦是fcos.同时获得正弦值和余弦值可以使用FSINCOS.

正切和反正切的指令是FPTAN和FPATAN。

FPATAN指令使用两个隐含的源操作数,计算角值ST1/ST0的反正切值。把值保存在ST1,然后弹出ST0,值移动到ST0位置。

不过就是在使用三角函数之前,需要把值转换为弧度。

弧度=(角度*pi)/180

正弦示例

.extern printf ;//调用外部的printf函数
.section .data
   fmt: .ascii  "result is: %f \n"
degree1:
   .float 30.0
val180:
   .int 180
.section .bss
   .lcomm radian1, 4
   .lcomm result1, 8
   .lcomm result2, 8
.section .text
.globl _start
_start:
   nop
   finit;//初始化fpu
   flds degree1;//加载角度90
   fidivs val180;//除以180,保存在st0
   fldpi;//加载π到st1
   fmul %st(1), %st(0) ;//乘以π得到弧度,在st0中。
   fsts radian1;//保存弧度到radian1中
   fsin;//求正弦,保存在st0
   fstl result1;//正弦结果保存到result1中。

   movq $fmt,%rdi;//调用printf函数
   movq result1,%xmm0
   call printf

   flds radian1;//加载弧度到st0
   fcos;//计算余弦,
   fstl result2;//

   movq $fmt,%rdi
   movq result2,%xmm0
   call printf

   movq $60,%rax
   syscall
           

as -g -o trigtest.o trigtest_att.s

ld -o trigtest trigtest.o -lc -I /lib64/ld-linux-x86-64.so.2

正弦余弦示例

.extern printf ;//调用外部的printf函数
.section .data
   fmt: .ascii  "result is: %f \n"
degree1:
   .float 30.0
val180:
   .int 180
.section .bss
   .lcomm sinresult, 8
   .lcomm cosresult, 8
.section .text
.globl _start
_start:
   nop
   finit
   flds degree1
   fidivs val180
   fldpi
   fmul %st(1), %st(0)
   fsincos
   fstpl cosresult
movq $fmt,%rdi
   movq cosresult,%xmm0
   call printf

   fstl sinresult
   movq $fmt,%rdi
   movq sinresult,%xmm0
   call printf

   movq $60,%rax
   syscall
           

as -g -o sincostest.o sincostest.s

ld -o sincostest sincostest.o -lc -I /lib64/ld-linux-x86-64.so.2

继续阅读