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!
This line is a problem:
First of all,
outputis 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: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):