Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

external assembly file in visual studio

I searched and found I can not use __asm in x64 in visual studio. Instead I have to use an external assembly file.

How can I add external assembly file to my win32 console project?

How can compile them?

Can you explain step by step.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How to build a mixed-source x64-project with a x64 assembly file in Visual Studio:

1) Start Visual Studio (Community) 2015 and choose FILE - New - Project.

Screenshot

2) In the next window choose Win 32 Console Application.

Screenshot

3) You get a confirmation. Click on Next >.

Screenshot

4) In the next window you can accept the default settings. Click on Finish.

Screenshot

5) Make sure, that the project is highlighted in the Solution Explorer and and choose PROJECT - Build Customizations... from the menu.

Screenshot

6) In the next window tick masm(.targets,.props) and click on OK.

Screenshot

7) Choose Build - Configuration Manager...

Screenshot

8) Change the Active solution platform to x64

Screenshot

9) Create callee.asm: PROJECT - Add New Item.

Screenshot

10) In the next window choose C++File(.cpp) and - IMPORTANT! - give it a name with an .asm extension. Click on Add.

Screenshot

10) Now check if the .asm file has the right properties. In the Solution Explorer right-click on the file and choose Properties.

Screenshot

11) In the Property Page you should see at least:

Excluded From Build    (empty) or No
Item Type              Microsoft Macro Assembler

Screenshot

Under Command Line ensure that ml64.exe is chosen as the assembler.

Screenshot

Click on OK.

12) Now you can fill the files with content.

ConsoleApplication1.cpp:

#include <iostream>
using namespace std;

extern "C" void hello_from_asm();

int main()
{
    cout << "Hello from CPP" << endl;
    hello_from_asm();
    return 0;
}

callee.asm:

PUBLIC hello_from_asm
EXTERN puts:PROC

.data

    hello1 db "Hello from ASM.",0

.code

hello_from_asm PROC
    push rbp
    mov rbp, rsp
    sub rsp, 32                 ; Shadow Space
    and spl, -16                ; Align stack at 16

    lea rcx, hello1
    call puts

    leave                       ; Restore stack (rsp) & frame pointer (rbp)
    ret
hello_from_asm ENDP

END

13) Build the .exe

Screenshot

and run it with CTRL-F5.

The application will be opened in a new window.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...