打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
vc 链接asm文件

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
(转)Visual C Inline ASM 内联汇编
内联汇编基础知识
VC++内联汇编(MSDN相关内容完整翻译)
Win32汇编开发环境介绍和RadAsm简明教程
在Visual C++中使用内联汇编
鼠标屏幕取词的原码
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服