0
.section .data
input:
.ascii "%f"
.section .bss
there:
.zero 4
.section .text
.global main
main:
pushl $there
push $input
call scanf
pushl there
push $input
call printf
call exit

Hello, i am struggling with printing float number in assembler. When I use format %d, number is shown correctly homever, I want to print float number. When I write for ex. 24.14 to my input i get strange results: 977035033722559154240093725614126257117369595496927687166085442682436777626733727512453867836858602465787904.000000. Homever when I change to %d format and write to input for ex. 24 the result is correct: 24.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
kkkkkkkkkk
  • 11
  • 2
  • 3
    `printf`, being variadic, expects a `double` rather than a `float`. You'll have to convert it and push it as two dwords (with another dword pushed first for alignment). Or change the `scanf` format string to `%lf` and allocate 8 bytes for `there`. Follow the calling conventions documented in the SysV ABI. – Nate Eldredge Apr 15 '21 at 19:10
  • Check out [what gcc does](https://godbolt.org/z/sEzPK4dMf), using `flds` and `fstpl` to load as `float` and store back as `double`. Also notice how it keeps the stack aligned to 16 bytes for every `call` - [that's required](https://stackoverflow.com/questions/21748272/stack-alignment-on-x86). – Nate Eldredge Apr 15 '21 at 20:32

0 Answers0