Linux – floating point in assembly linux

assemblylinux

Could you please give me a assembly code in linux(ubuntu 9.04) to print floating point number?

Best Solution

There's lots of ways this question can be answered without you being more specific (architecture, syscalls or libc, gas or nasm, output format, etc), but here is one way (x86, libc, nasm)

bits 32

section .text

extern printf
global main

main:
  fldpi ; load pi onto FPU stack
  sub esp, 8 ; make space on CPU stack
  fstp qword [esp] ; pop from FPU stack and store on CPU stack (parameter 2 of printf)
  push format ; stack the format string (parameter 1 of printf)
  call printf
  add esp,12 ; restore stack (4 btyes address + 8 bytes float)
  xor eax,eax ; set eax (return value) to 0
  ret ; return to libc

section .data

format: db "%.20g",10,0

outputs : 3.141592653589793116