I'm trying to a write a C++ program that can call assembly functions that can call C++ functions, so I wrote this test:
Main.cpp
#include<stdio.h>
extern "C" {
int bar();
}
extern "C" int foo() {
printf("I've been called!\n");
return 0;
}
int main() {
bar();
return 0;
}
bar.asm
.586
.MODEL FLAT, C
.CODE
extrn foo:dword
bar PROC
jmp foo
bar ENDP
END
The final program looks fine in the disassembler, but it segfaults on "jmp foo." How can I fix this?
A possibly related issue is that I was only able to get this linking at all by building bar.asm in WinASM, and then adding bar.obj as an additional dependency in Visual Studio Express. I tried following a guide for how to build assembly in Visual Studio, but it did not work.
If you want your function called on assembly, use GetProcAddress. And then call it. Dont forget to balancing the stack, except your function auto balancing it. But most stdcall type function did not balancing the stack. Should I explain about the push and pop too? Just in case you did not know. Im afraid hutch will come to you and warn you.
Quote from: Darmani on November 25, 2011, 05:55:59 AM
jmp foo
What kind of jump is generated here?
FF25 xxxxxxxx jmp dword ptr ds:[foo]
This obviously wont work, you want "E9 xxxxxxxx". Change the "extrn foo" definition.
Thanks drizz!