Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

FSTP : I can't understand exactly what it does.

169 views
Skip to first unread message

Alex Duaner

unread,
Aug 27, 2003, 3:00:43 PM8/27/03
to
Hello everyone.
I'm currently learning ASM (since 4 month), and i'm working on Real
arithmetic with FPU.

here's a little snippet of a code that i can't understand:

MOV DWORD PTR SS:[EBP-4], ESI ; mov ESI in EBP-4 (ESI=0x17)
ADD ESP, -8
FILD DWORD PTR SS:[EBP-4] ; st0=23 decimal (0x17)
MOV BL, 1
MOV EDI, 3
FSTP QWORD PTR SS:[ESP] ; ESP=12FD58

Here, at the FSTP command, St0 became empty, but the thing i couldn't
understand is there, on the stack:

12FD58: 00000000
12FD5C: 40370000

I couldn't understand what is this number stored on the stack from
0x12FD58 to 0x12FD5C.
So, what does this number means exactly ?
How is this number generated ?
Is it possible to convert it manually ?

I've tried to convert it with my calculator, but i can't retrieve 23d
I'm asking this question because this number (0x4037) is used later on
the program.

Thank you very much.

--------------------
I'm truly sorry for my poor english, but i'm practising...

PS: remove nospamming- if you want to mail me

Alex Duaner.

Shill

unread,
Aug 27, 2003, 8:19:11 PM8/27/03
to
> here's a little snippet of a code that i can't understand:
>
> MOV DWORD PTR SS:[EBP-4], ESI ; mov ESI in EBP-4 (ESI=0x17)
> ADD ESP, -8
> FILD DWORD PTR SS:[EBP-4] ; st0=23 decimal (0x17)
> MOV BL, 1
> MOV EDI, 3
> FSTP QWORD PTR SS:[ESP] ; ESP=12FD58
>
> Here, at the FSTP command, St0 became empty, but the thing i couldn't
> understand is there, on the stack:
>
> 12FD58: 00000000
> 12FD5C: 40370000
>
> I couldn't understand what is this number stored on the stack from
> 0x12FD58 to 0x12FD5C.
> So, what does this number means exactly ?
> How is this number generated ?
> Is it possible to convert it manually ?
>
> I've tried to convert it with my calculator, but i can't retrieve 23d
> I'm asking this question because this number (0x4037) is used later on
> the program.

The FILD instruction converts a signed-integer in memory
to double-extended-precision format and pushes the value
onto the x87 register stack. The value can be a 16-bit or
32-bit integer value. Signed values from memory can always
be represented exactly in x87 registers without rounding.

The FILD instruction will write 23 in double-extended-precision
format (80 bits) to ST0.

Then the FSTP instruction will write 23 in double-precision format
(64 bits, i.e. two words) to [ESP].

http://courses.ece.uiuc.edu/ece291/archive/archive-sum97/lecture/l24.html

23 = 0x17 = 10111 = 1.0111 * 2^4

Therefore
S = 0
E = 0x3FF + 4 = 0x403 = 100 0000 0011
M = 0111 0000 0000 0000 ...

Concatenate S, E and M:
0100 0000 0011 0111 0000 0000 0000 ...

0x4037000000000000 = 23 in the IEEE 754 double-precision format.


pacm...@hotmail.com

unread,
Aug 27, 2003, 8:23:54 PM8/27/03
to
Alex Duaner <tzcorp...@nospamming-hotmail.com> wrote:
> Hello everyone.
> I'm currently learning ASM (since 4 month), and i'm working on Real
> arithmetic with FPU.

> here's a little snippet of a code that i can't understand:

> MOV DWORD PTR SS:[EBP-4], ESI ; mov ESI in EBP-4 (ESI=0x17)
> ADD ESP, -8
> FILD DWORD PTR SS:[EBP-4] ; st0=23 decimal (0x17)

The statement above puts an integer DWORD value on the stack
(converting it to floating point)

> MOV BL, 1
> MOV EDI, 3
> FSTP QWORD PTR SS:[ESP] ; ESP=12FD58

The statement above stores the real at the top of the coprocessor stack
to a QWORD in memory (in IEEE double precision format.)

> Here, at the FSTP command, St0 became empty, but the thing i couldn't
> understand is there, on the stack:

> 12FD58: 00000000
> 12FD5C: 40370000

23.0 is represented as 40 37 00 00 00 00 00 00 in IEEE double precision.
So the code is just converting 23 to 23.0 or in C.

int i = 23;
double f = (double) i;

See chapter 6 of my online book for more on floating point format.

http://www.drpaulcarter.com/pcasm/

[snip]

> Alex Duaner.


--
Paul Carter

Bx.Cornwell

unread,
Aug 27, 2003, 9:02:19 PM8/27/03
to
> 23 = 0x17 = 10111 = 1.0111 * 2^4
>
> Therefore
> S = 0
> E = 0x3FF + 4 = 0x403 = 100 0000 0011
> M = 0111 0000 0000 0000 ...
>
> Concatenate S, E and M:
> 0100 0000 0011 0111 0000 0000 0000 ...
>
> 0x4037000000000000 = 23 in the IEEE 754 double-precision format.
>

damn.. that's not near as messy as my notepad... but then... i had just
started, a week ago, with IEEE 754 format (self-taught)... so i hadn't the
time to write up a few example conversion problems for myself...

Bx.C, shiragaseinen

Bx.Cornwell

unread,
Aug 27, 2003, 8:59:29 PM8/27/03
to
---snip it!---

> MOV DWORD PTR SS:[EBP-4], ESI ; mov ESI in EBP-4 (ESI=0x17)
> ADD ESP, -8
> FILD DWORD PTR SS:[EBP-4] ; st0=23 decimal (0x17)
> MOV BL, 1
> MOV EDI, 3
> FSTP QWORD PTR SS:[ESP] ; ESP=12FD58
>
> Here, at the FSTP command, St0 became empty, but the thing i couldn't
> understand is there, on the stack:
>
> 12FD58: 00000000
> 12FD5C: 40370000
>
> I couldn't understand what is this number stored on the stack from
> 0x12FD58 to 0x12FD5C.
> So, what does this number means exactly ?
> How is this number generated ?
> Is it possible to convert it manually ?
---snip it!---

bear with me.. this was a little hard, as it was my first attempt at doing
this...

that first FPU line:


FILD DWORD PTR SS:[EBP-4]

....took a 32 bit integer from memory and placed it into ST0... we know from
looking at the line before, that the value at that memory location is
0x00000017, or 23 decimal

that next FPU line:


FSTP QWORD PTR SS:[ESP]

....pops ST0 off the FPU stack, placing that value, in floating point format,
into a 64-bit memory location,...

so....

4037000000000000 is a double precision floating point
(qword-sized ((64-bit)) floating point )

and for the conversion... here's the manual way to do it...
no calculators were harmed in the conversion of these bits!

in binary (chopped into pieces: sign bit, biased exponent, significand)
0 10000000011 0111000000000000000000000000000000000000000000000000

sign bit = 0 = positive number

biased exponent bits (11 of them for a double precision float)
--> not all of them are 0 so it is a normalized number

note i said biased... subtract 01111111111 from it to get the unbiased

10000000011
-1111111111
-----------
00000000100 = 4 ...so we have 2 ^ 4

significand begins with 0111 (all the rest 0, so ignore the rest)
we always place a "1." at the beginning...
and treat somewhat similar to scientific notation

1.0111000... = 1.0111 (binary)

thus, we have

1.0111 * 2^4 = 1.0111(bin) * [ 10(bin) ^ 4 ] = 10111. = 23

the best way to really think of the 2^4 in this calculation, is to think of
it as a binary 10^4... just move that decimal place!!!

thus, i have successfully converted your number from IEEE Standard 754
Floating Point Format, back into human-readable decimal... (and to think,
this was my first ever real-world attempt at doing so, well, besides the one
example in Intel's IA-32 Software Developer's Manual, Volume 1 (Table 4-5 on
page 4-14)

Bx.C, shiragaseinen

PS. (for some reason, i feel i would have to try a few non-integer numbers,
before i really feel confident about doing these conversions myself... so...
i wrote a few lines in grdb to test the answer and it came out correct...)


Robert Wessel

unread,
Aug 27, 2003, 11:01:17 PM8/27/03
to
Alex Duaner <tzcorp...@nospamming-hotmail.com> wrote in message news:<ulupkv4g3pj4m3amt...@4ax.com>...

> Hello everyone.
> I'm currently learning ASM (since 4 month), and i'm working on Real
> arithmetic with FPU.
>
> here's a little snippet of a code that i can't understand:
>
> MOV DWORD PTR SS:[EBP-4], ESI ; mov ESI in EBP-4 (ESI=0x17)
> ADD ESP, -8
> FILD DWORD PTR SS:[EBP-4] ; st0=23 decimal (0x17)
> MOV BL, 1
> MOV EDI, 3
> FSTP QWORD PTR SS:[ESP] ; ESP=12FD58
>
> Here, at the FSTP command, St0 became empty, but the thing i couldn't
> understand is there, on the stack:
>
> 12FD58: 00000000
> 12FD5C: 40370000
>
> I couldn't understand what is this number stored on the stack from
> 0x12FD58 to 0x12FD5C.
> So, what does this number means exactly ?
> How is this number generated ?
> Is it possible to convert it manually ?
>
> I've tried to convert it with my calculator, but i can't retrieve 23d
> I'm asking this question because this number (0x4037) is used later on
> the program.


Breaking the stored number up there is:

- a one bit sign (0 = positive)

- an 11 bit exponent (10000000011 = 1027 = +4)

- an implied 1. bit which is not physically in the number

- the significand (binary .01110000...)

Combining those parts we have 1.0111000...*(2**(+4)) which is 23 (note
the mixed bases, the 1.0111000 is binary, the 2**4 is decimal).

Alex Duaner

unread,
Aug 28, 2003, 9:03:47 AM8/28/03
to
I wish to thank you for your replies and your links, it's helped me a
lot.

Regards, Alex

0 new messages