Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C inline assembly of x86 fbstp instruction

Was wondering how to inline a usage of fbstp on a 32 bit I86 architecture. I tried something like

int main( )
{
    double foo = 100.0;

    long bar = 0;

    asm( "pushl %1; fbstp %0"
         : "=m"(bar)
         : "r"(foo)
       );

    ...

But bar is unchanged. I have tried reading everything I can find on this but most example simply do things like add two integers together. I can’t find any that talk about pushing operands onto the stack and what I should be doing when an instruction like fbstp writes 80 bits of data back to memory ( i.e. what C type to use ) and how to specify it in the asm syntax.

Also on x86-64 there seems to be a pushq and no pushl but fbstp still exists whereas fbstq does not. Is there some other magic for 64 bit.

like image 327
David HUnter Avatar asked Dec 15 '25 11:12

David HUnter


1 Answers

There's an example here: http://bap.ece.cmu.edu/download/bap-0.1/VEX/test/test-i386.c

which seems to suggest doing something like this:

unsigned short bcd[5];
double a;

asm("fbstp %0" : "=m" (bcd[0]) : "t" (a) : "st");
like image 77
Paul R Avatar answered Dec 17 '25 02:12

Paul R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!