Can anyone tell me how I can increment the stacks I want to compare?

for example:


i++ //increment i

basically, I was to compare ST(0) to ST(0) and ST(0) to ST(1) and ST(0) to ST(2) then....
ST(1) to ST(0) and ST(1) to ST(1) and ST(1) to ST(2)

Posted on 2010-07-23 19:46:12 by l flipboi l
There's only ST(0) thru ST(8), and its rare to use more than a few of them at once.
C does not refer to individual FPU stack items by their Index, it generates specific code to access specific fpu stack locations.
I suggest you teach yourself to use the phrase "fpu stack" rather than simply "stack" to avoid confusion.
Posted on 2010-07-23 20:27:32 by Homer
I guess he means increasing the index of the item.
l flipboi l, I think you should learn about loops, first.
Posted on 2010-07-24 10:56:53 by ti_mo_n
Nice tutorial on FPU here:


There are several "compare" instructions. The most commonly available - introduced first - "fcom" - is a bit of a PITA to use, as it does not set CPU status flags directly. "lahf" will help! Instructions introduced on later processors do set the flags, and are "easier". So... what are the "hardware requirements" for this program?

As I understand the question here, "flip" wants to do something like "st(N)", "inc N". AFAIK, there's no such instruction - but the FPU does something similar all by itself. You may, in fact, want to avoid this by using the "pop" varients of FPU instructions (the ones ending in 'p'). As Homer points out, there are a limited number of FPU "registers" or "stack positions" - st0 through st7 (I'll use Nasm syntax - without the "()" - to avoid the accursed smileys that bit Homer :) If you "run off the top of the stack", it causes an exception. Could crash your program, but most(?) OSen handle it by ignoring it - the "top value" will simply be lost. You want to avoid it, in any case.

The FPU is a little bit "weird" (IMO) - read up on it!

For single-precision (32 bit), you can address your array as "" - just like an integer array. For double-precision (64 bit), use "", assuming ecx is the "item number". Probably best to ignore "extended-precision" (80 bit) for now.

Hint: "printf", if you're using it, always expects double-precision! Even though the format string says "%f", it's a  double. Not knowing this will result in printing some weird numbers! :)

Give it a shot, and we'll try to help with any specific problems you run into.


Posted on 2010-07-24 13:00:51 by fbkotler
Thanks, Guys!

We are going by the book x86 by Kip Irvine.

I've been able to code the SWAP procedure, but I seem to be running into a problem with the conditions.

Is this right? assuming that the FPUstack looks like this....

ST(0) = 1.3
ST(1) = 5.1

I'm trying to write a code that sorts (BubbleSort) and once the sorting is done ST(0) will have the highest value so when I push it into a VAR it'll be in ascending order.

This is the condition i'm having trouble with...Let me know what adjustments I need to make...i understand you mentioned that the "CPU status flags are not set directly," but not sure how to do that.  

FCOM               ;ST(0) compare with ST(1)
JNAE  Load       ;Jump if ST(0) greater than ST(1)
call Swap         ;if ST(0) less than ST(1)


I will read the link you sent me once i get back.

Posted on 2010-07-24 15:38:15 by l flipboi l
I could be mistaken about this - haven't used the FPU that much - but I think you're going to have to do:

FCOM              ;ST(0) compare with ST(1)
JNAE  Load      ;Jump if ST(0) greater than ST(1)
call Swap        ;if ST(0) less than ST(1)

I was wrong when I said "lahf" - you want "sahf" - so I'm probably wrong on the rest of it, too! (or use FCOMI, which does set CPU flags)

I haven't read Kip Irvine's book, so I don't know what he says about FPU. Ray  Filiatreault's tut, link above, is good - I think AoA covers FPU pretty well, too (use the obsolete dos version unless you want HLA syntax).


Posted on 2010-07-24 17:14:02 by fbkotler
Thanks, Frank! I was reading the website earlier and it looks like there are some good info.
Posted on 2010-07-25 01:08:56 by l flipboi l
I've become somewhat of an expert in FPU code and FPU optimizations, but I fail to understand MMX, something in my brain does not jive yet.
Maybe I just never found the right tutorial, maybe I am making beans from curds, what do you say?
Posted on 2010-07-25 08:05:03 by Homer