assemblyprintfgnu-assemblerattfpu

How to print floating point numbers from assembly?


I'm trying to print a floating point number by calling printf but it seems to always just print the pi value (3.1415) although the result, which is supposed to be the area of a circle, is supposed to be moved to the pi variable after being calculated.

.section .data
    value:
        .quad 0
    result:
            .asciz "The result is %lf \n"
    pi:
        .double 3.14159

.section .bss
.section .text
.globl _start
.type area, @function
area:

    nop
    imulq %rbx, %rbx
    movq %rbx, value
    fildq value
    fmul pi                           # multiply r^2 by pi
    fst  pi                           # Store result to pi
    movupd pi, %xmm0                  # move result to xmm0
    nop
    ret

_start:

    nop
    movq $2, %rbx
    call area                 # calculate for radius 2
    leaq result, %rdi         
    movq $1, %rax             # specify only one float value
    call printf                 
    movq $0, %rdi             # Exit
    call exit                     
    nop

I always get 3.1415 back. I dont know why as it's supposed to be overwritten by the fst instruction.


Solution

  • You need to add a size suffix to your floating point operations if they happen to use memory operands. Otherwise, the GNU assembler will implicitly use single precision which is not what you want. To fix your code, change

    fmul pi                           # multiply r^2 by pi
    fst  pi                           # Store result to pi
    

    to

    fmull pi                           # multiply r^2 by pi
    fstl  pi                           # Store result to pi
    

    Some other remarks about your code: