News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Bidirectionally linking MASM and C++

Started by Darmani, November 25, 2011, 05:55:59 AM

Previous topic - Next topic

Darmani

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.

Farabi

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.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

drizz

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.
The truth cannot be learned ... it can only be recognized.