Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making assembly function inline in x64 Visual Studio

I know that MSVC compiler in x64 mode does not support inline assembly snippets of code, and in order to use assembly code you have to define your function in some external my_asm_funcs.asm file like that:

my_asm_func PROC
    mov rax, rcx
    ret
my_asm_func ENDP

And then in your .c or .h file you define a header for the function like that:

int my_asm_func(int x);

Although that solution answers many concerns, but I am still interested in making that assembly code function to be inline, in other words - after compilation I don't want any "calls" to my_asm_func, I just want this piece of assembly to be glued into my final compiled code. I tried declaring the function with inline and __forceinline keywords, but nothing seems to be helping. Is there still any way to do what I want?

like image 948
user1483597 Avatar asked Dec 18 '16 11:12

user1483597


People also ask

What is inline assembly code?

In computer programming, an inline assembler is a feature of some compilers that allows low-level code written in assembly language to be embedded within a program, among code that otherwise has been compiled from a higher-level language such as C or Ada.

What is the use of inline assembly?

The uses of inline assembly include: Writing functions in assembly language. Spot-optimizing speed-critical sections of code. Making direct hardware access for device drivers.

Does C++ have inline assembly?

You can use the inline assembler to embed assembly-language instructions directly in your C and C++ source programs without extra assembly and link steps. The inline assembler is built into the compiler, so you don't need a separate assembler such as the Microsoft Macro Assembler (MASM).


1 Answers

Since the title mentions Visual Studio and not MSVC, I recommend installing Clang via the Visual Studio Installer. It can be used just like MSVC without needing to configure custom build tasks or anything and it supports inline assembly with Intel syntax and variables as operands.

Just select "LLVM (clang-cl)" in Platform Toolset from the General section of the property pages in your project and you're good to go.

like image 175
Big Temp Avatar answered Oct 04 '22 16:10

Big Temp