Build a C++ project with external procedure written in ASM

>> Tuesday, October 6, 2009

You have an external procedure written in ASM, and you want to use it in C++ code.
I use MASM and VC2008 to build this project.

This is file add.asm


.686
.model flat
public _add
.code
_add proc
push ebp
mov ebp, esp

mov eax, dword ptr [ebp+8]
add eax, dword ptr [ebp+12]

mov esp, ebp
pop ebp
ret
_add endp
end


Compile this file with command:


ml /coff /c add.asm


Here's the file main.cpp



#include "stdio.h"
#include "tchar.h"

extern "C" int add(int,int);

int main()
{
int a = 1, b = 2;
printf("%d + %d = %d", a, b, add(a,b));
return 0;
}


The last step is :


cl /EHsc /main.cpp /link /RELEASE add.obj


0 comments: