分享

vc 链接asm文件

 quasiceo 2014-01-13

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

asked Oct 24 '13 at 8:15
huyhu
11


 
Have you tried removing the underscore? –  Michael Oct 24 '13 at 8:38

 
yes i have tried. but same result. –  huyhu Oct 24 '13 at 9:19

 
Did you check which symbols your asm file exports? YOu should see the name that the main file expects. –  Devolus Oct 24 '13 at 9:37

 
@Devolus how can i check it? i mean symbols –  huyhu Oct 24 '13 at 13:30

 
I don't know which environmont you are using, but if you installed Visual Studio you should have dumpbin which is similar to objdump under Linux. Whith this you can insepct all aspects of your lib/obj files. dumpbin /SYMBOLS your_asm.obj should show you all names. –  Devolus Oct 24 '13 at 14:26
add comment

2 Answers

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
answered Oct 24 '13 at 9:43
Devolus
8,95131033


 
thanks for help i have tried like this but no change. how can compile a driver with this external object? my output will be .sys at least –  huyhu Oct 24 '13 at 13:27

 
Might be that there are different rules for drivers. Did you test with a simple exe file first, if you can call an asm function? Does this also not work, or only if you try to compile a driver? Could be that the link step is different. –  Devolus Oct 24 '13 at 14:28
add comment

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多