1

I have made a .lib file using visual studio 2010 and now I want to use it in masm. How can I do that? need help. I tried to look it on the internet but couldn't find any precise answer on how to do this.

here is the link to .lib file that I created using Visual C++ 2010. https://www.dropbox.com/s/pzdd35ktolfl48x/MathFuncsLib.lib

This file contains only a function that takes two integers as arguments and returns their sum. I need to use this function in one of my program written in intel assembly using masm.

Need help in this problem.

1
  • You've left out important information like which calling convention you're using. Instead of linking to the lib file (which is a binary), show us the C code and the assembly code and the compiler/assembler options you're using when building.
    – Michael
    Commented May 8, 2014 at 19:08

3 Answers 3

1

I was too optimistic saying that there are many examples for a MASM-C-combination in the net. Here a step-by-step instruction for Visual Studio Express 2010 (no Masm32):

MathFuncsLib.cpp:

extern "C" {
    int Addition (int a, int b) { return a + b; }
    int Subtraction (int a, int b) { return a - b; }
    int Multiplication (int a, int b) { return a * b; }
    int Division (int a, int b) { return a / b; }
} // end of extern "C"

MathFuncsLib.asm:

.MODEL flat, stdcall

includelib kernel32.lib
ExitProcess PROTO STDCALL, :DWORD

includelib msvcrt.lib
printf PROTO C, :VARARG

includelib MathFuncsLib.lib
Addition PROTO C, :SDWORD, :SDWORD
Subtraction PROTO C, :SDWORD, :SDWORD
Multiplication PROTO C, :SDWORD, :SDWORD
Division PROTO C, :SDWORD, :SDWORD

.data
    arg1 SDWORD 100
    arg2 SDWORD 10
    result SDWORD ?

    fmt1 db "Addition: %u",10,0
    fmt2 db "Subtraction: %u",10,0
    fmt3 db "Multiplication: %u",10,0
    fmt4 db "Division: %u",10,0

.code
main PROC

    invoke Addition, arg1, arg2
    mov result, eax
    invoke printf, ADDR fmt1, result        ; printf (fmt1,result)

    invoke Subtraction, arg1, arg2
    mov result, eax
    invoke printf, ADDR fmt2, result

    invoke Multiplication, arg1, arg2
    mov result, eax
    invoke printf, ADDR fmt3, result

    invoke Division, arg1, arg2
    mov result, eax
    invoke printf, ADDR fmt4, result

    invoke ExitProcess, 0

main ENDP

END main

MathFuncsLib.cmd:

@ECHO OFF
call "C:\Compiler\Visual Studio Express 2010\VC\vcvarsall.bat"
rem Adapt it with path to your 'vcvarsall.bat'

cl.exe MathFuncsLib.cpp /c
lib.exe MathFuncsLib.obj
ml.exe MathFuncsLib.asm /link /subsystem:console

MathFuncslib.exe

Good luck!

1
  • You sir are an absolute genius, I must admit. THANK YOU SO MUCH for your help. Commented May 11, 2014 at 9:21
0

Your MathFuncsLib.lib includes decorated symbols and references to other .libs.

MSVC "decorate" its C++-symbols, i.e. the symbols contain not only names but also arguments, types, etc. - the whole declaration. If you call DUMPBIN.EXE MathFuncsLib.lib /SYMBOLS you can see the symbols stored in the .lib and find:

067 00000000 SECT19 notype () External | ?Subtract@MyMathFuncs@MathFuncs@@SAHHH@Z (public: static int __cdecl MathFuncs::MyMathFuncs::Subtract(int,int))

?Subtract@MyMathFuncs@MathFuncs@@SAHHH@Z is the decorated symbol and (public: static int __cdecl MathFuncs::MyMathFuncs::Subtract(int,int)) its undecorated content. MASM can't automatically undecorate the symbols, so you must manually explore the symbol and take the whole symbol in a PROTO directive. A following equate is useful for later calls.

It isn't generally a good idea to mix C++ and Assembler especially when using objects. Better is to write a C-program (C and C++ are two different languages!) and to integrate those .libs into an Assembler program. There are many examples for this in the net.

I've got following code running:

test.asm:

include \masm32\include\masm32rt.inc        ; MASM32 headers especially 'pr2'

includelib msvcprtd.lib
includelib MathFuncsLib.lib

?Subtract@MyMathFuncs@MathFuncs@@SAHHH@Z PROTO SYSCALL
Subtract EQU <pr2 PTR ?Subtract@MyMathFuncs@MathFuncs@@SAHHH@Z>

.data
    int1 SDWORD 100
    int2 SDWORD 10
    result SDWORD ?

.code
main PROC

    invoke Subtract, int1, int2
    add esp, 8
    mov result, eax

    printf ("%i\n",result);

    invoke ExitProcess, 0

main ENDP

END main

test.cmd:

@ECHO OFF 
SET PATH=C:\masm32\bin; 
SET LIB=C:\TMP\;C:\Compiler\Visual Studio Express 2010\VC\lib; 
ml.exe /nologo /coff test.asm /link /subsystem:console /opt:noref /ignore:4044 /nologo 
test.exe

In my testcase C:\TMP is the path to MathFuncsLib.lib. C:\Compiler\Visual Studio Express 2010\VC\lib is the path to msvcprtd.lib.

7
  • I took the method to make that .lib file from this link: msdn.microsoft.com/en-us/library/ms235627.aspx And the add function mentioned here is the same one that I want to use in my assembly program. Commented May 8, 2014 at 22:10
  • Thank You for your code. I tested it and it ran perfectly but still i dont understand how this code is working. Im a newbie to importing files in masm, can you please explain what the following code does and how is it working? include \masm32\include\masm32rt.inc ; MASM32 headers especially 'pr2' includelib msvcprtd.lib includelib MathFuncsLib.lib ?Subtract@MyMathFuncs@MathFuncs@@SAHHH@Z PROTO SYSCALL Subtract EQU <pr2 PTR ?Subtract@MyMathFuncs@MathFuncs@@SAHHH@Z> Commented May 9, 2014 at 6:09
  • @user3071543: I edited the post accordingly. I tried my best. HTH
    – rkhb
    Commented May 9, 2014 at 7:04
  • thank you for the edit. It has helped me broaden my mediocre understanding of assembly. It will be really great if you can provide me a link for a tutorial that clearly describes how to write a program in C and integrate those .libs in an assembler program. P.S. I am using DOSBox. Commented May 9, 2014 at 7:35
  • for some reason when I try to do the same thing for add .. it gives me two errors. 1)error A2008: syntax error : EQU 2)error A2008: syntax error : Add Commented May 9, 2014 at 10:56
-1

Masm615 is a 16 bit assembler and is incompatible with .lib files generated with 32/64 bit compilers like VS. A better option would be to use a 16 bit compiler like Turbo C++.

1
  • This is not correct. The generation of a .lib file is not the job of an assembler or a compiler but the library manger (lib.exe). The point is whether this library manager can handle the various .obj files and whether the linker can handle the final .lib files. BTW: Masm615 can produce 32 bit code.
    – rkhb
    Commented May 11, 2014 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.