0

I have this code that is suppose to print out a floating point value using _printf:

extern _printf
global _main
section .data
mensaje: db 'The number is %f', 10, 0
numero1: db 34.25
_main:
    push numero1
    push mensaje
    call _printf
    add esp, 8

    ret

The output I get is:

The number is 0.000000

The output I expect is:

The number is 34.250000

What is wrong with my code, and what must be done to get the desired output?

EDITED:

The solution is:

extern _printf
global _main
section .data
  mensaje: db 'The number is %f', 10, 0
  numero1: dd 34.25
section .text
_main:
    sub esp, 8
    fld dword [numero1]
    mov ebx, numero1
    fstp qword [esp]
    push mensaje
    call _printf
    add esp, 12
    ret

The correct output is:

The number is 34.250000

@MichaelPetch Thank you very much. I am sorry to everybody, my english is bad.

Omar Murcia
  • 547
  • 2
  • 11
  • 26
  • 2
    and what is the problem? what is `_printf` (or where from) ... What is the question: How to use unknown external `_printf` import ? Or code it yourself ? You need to add more details so +Close for now. Also what kind of `float` (like IEEE 754 or other?, 16/32/64/80/128 bit other?). `db 12.52` is most likely truncated to `db 12` !!! **IEEE 754** single precision is 4 BYTES not just one !!! – Spektre Feb 10 '17 at 09:16
  • @spektre is right on. You can't store a floating point in a single byte. – David Hoelzer Feb 10 '17 at 09:21
  • try change `db 12.52` to `dd 414851ECh` if it works then you are using IEEE 754 32 bit floats. if you need to convert text to float try `_scanf` ... – Spektre Feb 10 '17 at 09:25
  • Also please do not post duplicate questions. Use edit instead. The down/Close/Delete Votes you got are for reason... If you read the comments users are guiding you to what info you need to add. Just code tells us nothing without context. **Not working** tells us nothing too... – Spektre Feb 10 '17 at 09:34
  • Thank you very much. I dont want to use IEEE 745, only decimal numbers. How to convert IEEE 745 to decimal? – Omar Murcia Feb 10 '17 at 16:04
  • see https://en.wikipedia.org/wiki/IEEE_754-1985 and [How do you print the EXACT value of a floating point number?](http://stackoverflow.com/a/18401040/2521214) but you can also use FPU for this .... – Spektre Feb 10 '17 at 17:13
  • 1
    also to comment someone you need to add `@nick` to your comment so user `nick` get notified of your message. If you want to avoid `float`s then you can use fixed point but for that you need to write your own print function as `gcc::conio/stdio _printf` does not know any fixed point variables ... only integer and floating at least to my knowledge. I think NASM should have some syntax to declare floating constants According to NASM doc this should work: `dd 12.52` beware `dd 1` is not the same as `dd 1.0` – Spektre Feb 10 '17 at 17:31
  • @OmarMurcia what do you mean by "decimal", why do you use the `%f` in printf format string then? (`%f` is IEEE 745 `float` - 4 byte floating point number). It's not clear, what do you want. – Ped7g Feb 10 '17 at 18:47
  • @Spektre I have edited my question. Thank you, your help is so useful. – Omar Murcia Feb 11 '17 at 00:15
  • @DavidHoelzer I want print a float number, but the output is ambiguous. – Omar Murcia Feb 11 '17 at 00:21
  • 2
    Okay, there are a few issues. First place code in the `.text` section, not the `.data` section. Above `_main:` add `section .text`. Next as others pointed out you need to define your float to at least a 32-bit float with `numero1: dd 34.25` . But even if you do all that you have forgotten one rule of _C_. When dealing with variadic functions like `printf` floats have to be [promoted to doubles](http://stackoverflow.com/questions/29442155/printing-floats-with-printf-in-x86-nasm-32-bit) (64-bit values) and that 64-bit value needs to be placed onto the stack – Michael Petch Feb 11 '17 at 04:40
  • 1
    And then lastly if you are using GCC v4.5 and later there is a requirement that function calls be aligned to a 16-byte boundary (not just a 4-byte boundary) – Michael Petch Feb 11 '17 at 04:40
  • 1
    On a side not `push numero1` doesn't push the value at `numero1` it pushes the address of `numero1` which is not what `printf` expects. – Michael Petch Feb 11 '17 at 04:45
  • @MichaelPetch Thank you very much, I will apply your corrections to my code, and I will tell you how I was. – Omar Murcia Feb 11 '17 at 05:40
  • 1
    @MichaelPetch Thank you so much!!!! I reached the solution to my problem. – Omar Murcia Feb 11 '17 at 06:22

0 Answers0