Question

I'm trying to learn a bit about floating point operations in NASM Assembly for 32-bit programs in Ubuntu.

I'm interested in getting a number's square root. This is what I tried:

SECTION .data
num: dd 100
var: dd 0

SECTION .text
global main
main:

fld dword [num]
fsqrt
fstp dword [var]
mov EAX,[var]

I was expecting EAX to be 10. But GDB says that it is 0x1be24630 (467813936), which by the way I'm not sure how to convert to something more readable - but I think that if the result was 10, it would actually show up as 10, no?

What am I doing wrong here? Or is 0x1be24630 actually 10?

Was it helpful?

Solution

What you want is FILD and FISTP:

fild dword [num]   ; load a dword integer and convert it to floating point
fsqrt
fistp dword [var]  ; convert to dword integer and store in var

Alternatively you could do it like this:

num: dd 100.0  ; note the .0, which makes this a floating point value

fld dword [num]    ; load single-precision floating point value
fsqrt
fistp dword [var]  ; convert to dword integer and store in var
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top