2

I'm trying to simply read a floating point value and print it using assembly x86 64. So, the value for the variable price, which I'm using as the buffer for the c function scanf doesn't change when I try to print it.

It'll print the value it was initially set to, so in the case of the code bellow, it prints 0.0, so the scanf function doesn't properly change the value for price.

To generate the executable, I'm using the command nasm -f elf64 test.asm && gcc -no-pie test.o -o test.

What am I doing wrong?

section .data
    price   dq  0.0

    fmt db "%.1f", 0

section .text
    global main
    extern printf, scanf

main:
    push rbp
    mov rbp, rsp

    mov rdi, fmt ;scanf first arg, format string
    mov rsi, price ;scanf second argument, this variable is supposed to change
    mov rax, 0
    call scanf
    
    movq xmm0, [price] ;in float point mode xmm0 holds the value to be printed
    mov rdi, fmt
    mov rax, 1 ;rax has to be set 1 to float point mode
    call printf

    leave
    ret
Jonas
  • 416
  • 4
  • 12

1 Answers1

3

The %f formatting specifier calls for a float, not a double. Either use %lf for double precision or adjust price to be a single precision float.

fuz
  • 88,405
  • 25
  • 200
  • 352
  • 4
    If you did `scanf` with single-precision, you'd need `cvtss2sd xmm0, [price]` because `printf("%f")` *does* take a `double` (*[How to print a single-precision float with printf](https://stackoverflow.com/q/37082784)*). I guess that's part of what you mean by adjusting `price` – Peter Cordes Jun 16 '23 at 17:58