Re: what is wrong with this little piece of code?

From: Frank Kotler (
Date: 02/01/04

Date: Sun, 1 Feb 2004 00:41:24 +0000 (UTC)

Akyl Tulegenov wrote:

> Dear All!
> What's wrong with this little piece of code? I am simply trying to print
> out the value of pi variable, but ain't getting any output!
> section .text
> global _start
> _start:
> mov eax, 4
> mov ebx, 1
> mov ecx, pi

What happened to edx? Sys_write wants the length in edx. If you call it
with some random value in edx, perhaps zero, you certainly won't get the
results you expect.

> int 0x80
> mov eax, 1
> mov ebx, 0
> int 0x80
> section .data
> pi: dq 1.0

Nasm converts your "dq 1.0" into IEEE<some number I forget> floating
point notation. Eight bytes, but they appear as pretty much "garbage" -
1.0 is 00 00 00 00 00 F0 3F. Mostly not printable characters (3F is
"?"). You could print these as hex values fairly easily, but if you want
to see "1.0" out of it (pretty inaccurate value for pi :), you've got
some processing to do. If you're allowed to call libraries, "printf" or
"ftoa" are your friends! If you're expected to write your own "ftoa",
the "fbstp" instruction stores the *integer* part of the float in st(0)
into a ten-byte buffer - nine bytes of packed BCD (each nibble is a
decimal digit - still needs to be converted to an ascii character), and
a "sign byte", 80h if it's negative, else 0. Not too bad to get a
printable string out of that.

Nice floating-point tutorial by Raymond Filiatreault here:

If that's more than you wanted to know, "just call printf". I'm usually
the last guy who would tell you "just use a library" (okay, next-to-last
after Betov :), but unless the "assignment" is to write your own "ftoa",
you don't wanna mess with it.