-1

I'm trying to use a printTime() function written in C to test the running time of my 32 bits NASM program.

void printTime(float time){
    printf("The cpu time is %e \n" , time);
}

Here is part of the NASM code:

push edi           <---------------- Here edi holds int 6580001
call printTime
pop edi

Here is the assembly code generated by GCC, and I use GDB to track every step:

push ebp
mov esp, ebp
sub 0x8, esp
flds 0x8(esp)     <-------  x/d ($esp+0x8) gives me 6580001, the correct number
sub 0x4,esp
lea -0x8(esp),esp
fstpl (esp)
push 0x80486f6
call 0x8048370 <printf@plt>  <--- it prints 9.220545e-39 here, which is wrong
add 0x10, esp
leave
ret

Can anyone please tell me why the printf function prints something wrong instead of 6.58001e+5 ? i appreciate it.

1
  • 2
    Well, you're passing an int and printTime expects a float. They have different binary representations. See here h-schmidt.net/FloatConverter/IEEE754.html and enter 0x646721 (your number in hex) as the "Hexadecimal representation"
    – Roddy
    Mar 7, 2015 at 22:10

1 Answer 1

1

You are loading an integer into the floating point register and expecting it to treated as the same value in floating point. That's not how IEEE754 floating point works, the numbers use a different encoding scheme.

The bit pattern formed by the integer 6580001 (0x00646701) is as shown in the below analysis chart. The floating point number is made up of a sign, biased exponent, and fractional part.

seee eeee efff ffff ffff ffff ffff ffff
0000-0000 0110-0100 0110-0111 0000-0001

First, let's deal with the sign. Being 0, that means the number is positive. That was the easy bit :-)

The exponent bits are all zero and, in IEEE754 encoding, you would normally subtract the bias of 127 and raise two to that power to get the multiplier.

However, an all-zero exponent is treated specially (these are denormalised numbers). First, the normal practice of adding one to the fraction bits (see below) is not done. Second, the multiplier is adjusted to be 2-126 rather than 2-127.

That makes the multiplier 1.1754943508222875079687365372222 x 10-38.

For the fractional bits, you would normally add one with their place values (reducing reciprocals of two) but, since it's denormalised, you skip adding the one:

110-0100 0110-0111 0000-0001
||   |    ||   |||         |
||   |    ||   |||         +- 1/8M  = 0.00000011920928955078125
||   |    ||   ||| (64K-4M)
||   |    ||   ||+----------- 1/32K = 0.000030517578125
||   |    ||   |+------------ 1/16K = 0.00006103515625
||   |    ||   +------------- 1/8K  = 0.0001220703125
||   |    ||       (2K,4K)
||   |    |+----------------- 1/1K  = 0.0009765625
||   |    +------------------ 1/512 = 0.001953125
||   |             (64-256)
||   +----------------------- 1/32  = 0.03125
||                 (8,16)
|+--------------------------- 1/4   = 0.25
+---------------------------- 1/2   = 0.5
                                      0.78439342975616455078125

To get the actual number, you take the product of that result and the multiplier calculated earlier, to get 9.2205004550049022573489420224302 x 10-39, which is the result you're seeing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.