Question

This is my first attempt at using assembly and I'm just trying to use the Intel Architecture instruction FABS. (Referencing this document on page 399).

This is simply supposed to clear the sign bit.

The little I know about assembly involves sources and destinations but I'm not seeing any reference to the notation for this instruction.

Below is one of my attempts at using it (using Visual studio 2012, C++):

double myabs(double x){
  __asm(fabs(x));
return x;
}

This particular attempt give the error C2400: inline assembler syntax error in 'opcode'; found '('


Please note that I want to use the assembly instruction and am not interested in other / "better" options that are available.

Was it helpful?

Solution

Several pointers: First - you're using gcc style inline assembly, in MS style you could use -

__asm{ ... }

Second - instructions aren't functions, so the parenthesis there are also wrong.

Last but most important - fabs doesn't take an argument, it just works on the top of the FP stack. You need to explicitly load your variable there first. Try this:

__asm {
    fld x
    fabs
    fstp x
}

Anyway, using old x87 instructions is probably not a good thing, it's probably quite inefficient - you should consider switching to an SSE solution, see - How to absolute 2 double or 4 floats using SSE instruction set? (Up to SSE4)

OTHER TIPS

With VC++, you don't enclose the assembly language in parentheses. Correct syntax would be more like:

__asm fabs

or:

__asm { 
    fabs
    // possibly more instructions here
}

In your specific case, you'd probably want something like:

__asm { 
    fload x // load x onto F.P. stack
    fabs    // take absolute value
    fstp x  // store back to x and pop from F.P. stack.
}

As far as source and destination go, floating point on an x86 uses a stack. Unless you specify otherwise, most instructions (other than load/store) take operands from the top of the stack and deposit results on the top of the stack as well. For example, with no operand given, fabs will take the absolute value of the operand at the top of the floating point stack and deposit the result back in the same place.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top