I am trying to call a C function from asm, but it doesn't seem to be working properly...
I'm pretty sure I have everything right...
In C:
#include
int __cdecl callee(void) { return 0x12345678; }
int __cdecl inasm(void);
int main(void)
{
printf("%X", inasm());
return 0;
}
In ASM:
global _inasm
extern _callee
_inasm:
jmp .later
db "inasm()" ; so I can find it in the disassembly
.later:
mov eax, _callee
ret
I run this, and I get displayed on the screen the address of callee(), and it is correct. However if I change the mov line to
call _callee
then the program crashes with an "Illegal Operation" message!
I look in the generated exe with a disassembler, and find that the call statement is not referring to the function callee() at all! Sometimes (depending on what other things I add to the assembly) it is referring to the padding bytes after _inasm, sometimes to a point earlier in the _inasm function (causing infinite recursion), once it was referring to the ')' in the db string I put in for identification purposes...
What am I doing wrong? If I call a function defined elsewhere in the asm, it works perfectly.
FYI, I am compiling the C with mingw, here is the commands:
nasm test.asm -o test.o -f coff
gcc test.o test.c -o test.exe -Wall -std=c99