سؤال

I'm struggling with some x86-64 assembly, and floating point numbers are giving me a headache. For instance, when I run this code :

section .data
     omega:  dq 2.0
     omega2: dq 3.0

section .text
global func

func: push rbp
      mov rgp, rsp

      FINIT

      FLD qword [omega]
      FLD qword [omega2]
      FADD st0, st0

      mov rsp, rbp
      pop rbp
      ret

This function is called from a C code like that : printf("%Lf \n", func() ); Unfortunately the result is some bizarre number... I tried adding two integers using FIADD, and it worked fine. I dug through a ton of material already, but maybe someone here can point me to decent FPU tutorial, or share her/his experience and wisdom :)

Wrapping up the essentials:

  • language: x86-64 assembler
  • assembler: nasm v. 2.09.04 installed from repositories
  • compiler (for C): gcc v. 4.5.2 (installed with Ubuntu)
  • OS: Ubuntu 11.04 64bit on Oracle VM
  • Host OS: Windows 7 SP1 64bit
  • Processor: Intel i5 - 2430M 64bit (checked twice :D )
  • Problem: FPU can't add two numbers :(

Just in case : in the end i hope to use FSINCOS and other fancy FPU instructions, but seeing as even simple addition fails...

Thanks all in advance!

هل كانت مفيدة؟

المحلول

Ok, so in the end it turned out that my problems were related to the fact, that FPU registers are organized as a stack, and i didn't pay enough attention to it which resulted in garbage and unwanted leftovers. Switching from "standard" to "pop" versions of instructions helped!

Anyway - thanks to all who bothered to read, it is much appreciated! :)

If anyone is interested - I was doing a class assignment where I had to compute motion of one planet from the view of another (geocentric model) in assembly. The final result translated to processing can be viewed here.

نصائح أخرى

The L qualifier to printf %f makes that argument a long double (80-bit float), which is not the assembly data type. Remove the L and it will default to a double (64-bit float) which is what you are computing.

Also, it should be

  FADD st1, st0

to add the two numbers together. Otherwise it will double the second value.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top