huyhu
huyhu

Reputation: 1

masm output .obj can not be linked with others

can anyone tell me how to link masm output with other object files. for example main.obj.

I am developing a windows kernel mode driver and I want to move inline asm blocks to seperate .asm files for further requirements and prevent further difficulties for example: x64 does not support inline asm.

So, i added vm.asm source file to project and from build customization i have selected masm. after that i have selected Microsoft Macro Assembler for Item Type. When i build project vm.obj file has created successfully for vm.asm file. But i cant call asm functions (proc) from C code. It should be link error i think build process not putting obj files together.

also i tried with stdcall and cdecl but result is same.

my vm.asm:

.386
.model flat, C
option casemap :none 

PUBLIC _get_vm

_get_vm PROC
      mov eax, 0101h
      db 0fh 
      db 01h 
      db 0c1h
      ret
_get_vm endp
END

here is my c call:

ULONG vm_id;
extern int _cdecl get_vm();

vm_id = get_vm();

error: project_ddk\main.obj : error LNK2019: unresolved external symbol _get_vm referenced in function _DispatchPnP@8

my ml.exe commandline:

ml.exe /c /nologo /safeseh /Zi /Fo"%(FileName).obj" /Fl"" /W3 /errorReport:prompt /Ta

Upvotes: 0

Views: 3352

Answers (2)

James
James

Reputation: 1019

I have never made C functions with masm but in normal procedures you need a text segment, It might need a text segment defined.

.386
.model flat, C
option casemap :none 

PUBLIC _get_vm
_TEXT SEGMENT
_get_vm PROC
      mov eax, 0101h
      db 0fh 
      db 01h 
      db 0c1h
      ret
_get_vm endp
_TEXT ENDS
END

EDIT: I just tried to make a simple procedure that works in C, i got this and this works;

MASM proc;

.386
.model flat, C
option casemap :none

PUBLIC func
_TEXT SEGMENT
func PROC
      mov eax, 2
      ret
func ENDP
_TEXT ENDS
END

C call;

#include <stdio.h>

int main(){
    int i = 5+func();
    printf("%d",i);
    return 0;
}

cmd;

masm -> ml -c -coff func.asm

C -> gcc prog.c func.obj -o main

output -> 7

Upvotes: 0

Devolus
Devolus

Reputation: 22094

Here is some sample that I used to test to link against C (hope it helps).

main.cpp:

#include <iostream>
#include <string.h>
#include <windows.h>

extern "C"
{
    void PopTest(void);
};

int main(int argc, char*arg[])
{
    PopTest();
    return 0;
}

test.asm

.486
.model flat, C
option casemap :none

.data

.code

;***********************
;
; Just a demo how to declare functions to be used from C
;
PopTest PROC

    push es
    xor eax,eax
    push eax
    pop es
    pop es

    mov eax, 2134
    push eax
    mov ebx, [esp]
    add esp, 04
    mov ecx, [esp-4]
    ret

PopTest ENDP

END

Custom build step in VS2008:

D:\Programme\masm32\bin\ml.exe /coff /c test.asm /Fo test.obj
copy test.obj Debug\test.obj
del test.obj

Upvotes: 1

Related Questions