views:

107

answers:

2

I would like to call the pow function from inline assembly. The problem is i'm getting error C2244: 'pow' : unable to match function definition to an existing declaration. I'm new to assembly so this may be a trivial question but how do i resolve this? I guess it has something to do with the compiler not beeing able to properly resolve the overload of pow. The following code fragment is causing the error:

do_POW:
  // push first argument to the stack
  sub   esp, size value_type
  fld   qword ptr [ecx]
  fstp  qword ptr [esp]

  // push second argument to the stack
  sub   esp, size value_type
  fld   qword ptr [ecx - size value_type]
  fstp  qword ptr [esp]

  // call the pow function
  call  pow
  sub   ecx, size value_type
  fstp  qword ptr [ecx]
  add   esp, 2 * size value_type
  jmp loop_start 
A: 

I found the solution. For anyone having similar problems: Create a pointer to the pow function in the C++ code and use this pointer in the inline assembly code:

double (*pow_func)(double, double) = pow;
__asm 
{
  call pow_func
}
schrödingers cat
A: 

It's not finding the symbol pow because the actual name of the library function is decorated in some way. Each platform has a standard for how the names get decorated, which may vary with what calling convention is used. Many (most?) platforms use a single underbar prefix as the usual decoration, so I would try:

call _pow

Alternatively, you can write a simple program like this:

#include <math.h>

double foo(double x) {
    return pow(1.5, x);
}

and look at the generated assembly code to see what the decoration convention is on your platform.

Stephen Canon
This was my first idea. The problem is that VisualStudio just showed "call pow" in the disassembly.
schrödingers cat
@schrödingers cat: When you have VisualStudio produce assembly code for you, it gives you assembly code that *it can't assemble*? That seems rather broken; I would report a bug if I were you.
Stephen Canon