views:

61

answers:

1

My code looks like this

_declspec(naked) void 
 f(unsigned int input,unsigned int *output)
{
 __asm{

  push dword ptr[esp+4]
  call factorial

  pop ecx

  mov [output], eax //copy result
  ret
 }
}

__declspec(naked) unsigned int
factorial(unsigned int n)
{
 __asm{

  push esi
  mov esi, dword ptr [esp+8]
  cmp esi, 1
  jg RECURSE
  mov eax, 1    
  jmp END

  RECURSE:
   dec esi 
   push esi
   call factorial
   pop esi
   inc esi
   mul esi

  END:
   pop esi
   ret
 }
}

Its a factorial function and I'm trying to output the answer after it recursively calculates the number that was passed in

But what I get returned as an output is the same large number I keep getting Not sure about what is wrong with my output, by I also see this error CXX0030: Error: expression cannot be evaluated

Thanks!

+3  A: 

This line is a problem:

mov [output], eax //copy result

First of all, output is a pointer, so this would only change the pointer's value, not its contents. Second, because of the __declspec(naked) declaration it wouldn't know where to find the value unless you set the stack frame appropriately. So you can write it like this:

mov ecx, [esp+8]   //read pointer value
mov [ecx], eax     //write result into pointer

Or you can set up the stack frame yourself and then you will be able to access the variable by name (you'd still need to add the level of indirection due to it being a pointer):

//at function start:
push ebp
mov ebp, esp

.... 

mov ecx, [output]
mov [ecx], eax

pop ebp
ret
interjay