Search code examples
clinkermasm

Using MASM to generate Object files and link them with MSVC Object Files


I want to link an assembly file that contains just one function with an object file generated from. I would like to know how to create .obj files in MASM and I also need to know how to create such a function. Is this sufficient for a function that adds two ints together?

intadd PROC int1:DWORD int2:DWORD
mov eax, int1
mov ebx, int2
add eax, ebx
intadd ENDP

If I create and link the obj files, can I do

int x = intadd(1,1);

to receive 2?

To sum it up: I need to know how to create .obj files from MASM if they contain a Macro as above and how to call the macros from a HLL if my code doesn't work.


Solution

  • I believe that a standard installation of Visual C++ will also install ml.exe and ml64.exe, both of which produce .obj files compatible with that version of Visual C++'s link.exe.

    What you can do is, once you've assembled your assembly file with ml /c asmfile.asm into an .obj file, in your .c file, add the line:

    extern int intadd(DWORD int1, DWORD int2);
    

    Compile your .c code with cl /c cfile.c, then link both .obj files into the final executable with link asmfile.obj cfile.obj /OUT:exefile.exe.

    Note that your assembly function is invalid, however, since once you're missing a ret statement - calling it will crash your program.

    If you're seeking information on how to integrate assembly files into a Visual C++ project in Visual Studio, this question has some information.