Search code examples
visual-studio-2010visual-c++assemblymasmmasm32

how to use a library in masm or more specifcally a .lib file?


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.


Solution

  • 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!