![]() |
|
#1
|
||||
|
||||
X64 inline asm
Hi all,
I discovered this for myself recently, and wanted to share. If this is already well known, disregard =) The Intel C/C++ compiler add-on for Visual Studio will allow you to still use inline assembly language on the X64 platform. The syntax and such is close enough to MSVC that modifications needed to code to compile existing code with the Intel compiler are minimal at best, IMO. -Fyyre |
The Following User Says Thank You to Fyyre For This Useful Post: | ||
Indigo (07-19-2019) |
#2
|
|||
|
|||
where can I download "Intel C/C++ compiler add-on for Visual Studio "?
|
The Following User Says Thank You to winndy For This Useful Post: | ||
Indigo (07-19-2019) |
#3
|
||||
|
||||
Quote:
-Fyyre |
The Following User Gave Reputation+1 to Fyyre For This Useful Post: | ||
winndy (04-09-2010) |
The Following User Says Thank You to Fyyre For This Useful Post: | ||
Indigo (07-19-2019) |
#4
|
|||
|
|||
on vdown for example
|
The Following User Gave Reputation+1 to Av0id For This Useful Post: | ||
winndy (04-09-2010) |
The Following User Says Thank You to Av0id For This Useful Post: | ||
Indigo (07-19-2019) |
#5
|
|||
|
|||
Quote:
Could you show us correct syntax for inline assembler which will Intel C++ Compiler accept ? -- Jump |
The Following User Says Thank You to jump For This Useful Post: | ||
Indigo (07-19-2019) |
#6
|
||||
|
||||
Quote:
__asm mov rbx, rax; or __asm { ... } emit format is: __asm __emit 0xCC; sample function: Code:
__declspec(naked) int __fastcall strcmpW(const wchar_t *s1, const wchar_t *s2) { __asm { push rdi push rsi mov rdi, rdx mov rsi, rcx or rcx, -1 xor rax, rax repne scasw not rcx mov rdi, rdx repe cmpsw xor rdx, rdx mov ax, [rsi - 2] mov dx, [rdi - 2] sub rax, rdx pop rsi pop rdi ret } } |
The Following 2 Users Gave Reputation+1 to Fyyre For This Useful Post: | ||
jump (04-09-2010) |
The Following User Says Thank You to Fyyre For This Useful Post: | ||
Indigo (07-19-2019) |
#7
|
|||
|
|||
So if i understand correct, the inline assmbler have to be also x64 not x86,right?
So you cant tak source with inline x86 assembler and think it will be compileable for x64 architecture. You have to rewrite it, right? -- Jump |
The Following User Says Thank You to jump For This Useful Post: | ||
Indigo (07-19-2019) |
#8
|
||||
|
||||
x86 or x64 depends upon your project (like Av0id said above).
Regarding re-writing of code, how big of task depends on the code itself... i.e. the above strcmpW function, he only require change of registers from e*x to r*x.... AFAIK the calling covention always __fastcall on X64, more simple than x86 assembly, imo. -Fyyre |
The Following User Says Thank You to Fyyre For This Useful Post: | ||
Indigo (07-19-2019) |
#9
|
||||
|
||||
Inline asm is forbidden in studio compiler only. Guess they are too lazy to implement it. GCC and intel support it just fine.
|
The Following User Says Thank You to Archer For This Useful Post: | ||
Indigo (07-19-2019) |
#10
|
||||
|
||||
I think if you are proposing to write a full function in asm as opposed to a small piece of inline code, then you have to be very careful with the prolog and epilog, and especially careful with the structured exception handling. In providing inline asm, Intel assume you are fully conversant with X64.
Git |
The Following User Says Thank You to Git For This Useful Post: | ||
Indigo (07-19-2019) |
#11
|
|||
|
|||
Quote:
|
The Following User Says Thank You to Av0id For This Useful Post: | ||
Indigo (07-19-2019) |
#12
|
||||
|
||||
There's some good info on MSDN.
Calling convention Parameter passing Exception handling The exception handling page has a link to MASM macros for Prolog and Epilog. THE site for driver writers, OSR, has some info too. There is good info there in all the subtopics on exception handling, prolog & epilog, stack usagae, etc. Git |
The Following User Gave Reputation+1 to Git For This Useful Post: | ||
The Following User Says Thank You to Git For This Useful Post: | ||
Indigo (07-19-2019) |
#13
|
|||
|
|||
For the inline assembler, keep in mind that it heavily "corrupts" the optimization of the surrounding C code (well, at least it always did for MSVC, donno about Intel, but would guess it's the same). When the compiler reaches the asm block, it's a "black box" for it... so it dumps all the register values into local variables, appends the assembler block... and then loads the register values back.
So, it's better to write the whole function in inline assembler - than just a part, in which case the result might be worse than keeping it all in C, because the rest of the function is optimized much worse than if it were all in C. It would be interesting to know what led Microsoft to bad inline assembler in x64... |
The Following User Says Thank You to gigaman For This Useful Post: | ||
Indigo (07-19-2019) |
#14
|
||||
|
||||
I agree gigaman. It even more true when you consider the difficulty in writing a true x64 full function properly.
Git |
The Following User Says Thank You to Git For This Useful Post: | ||
Indigo (07-19-2019) |
#15
|
|||
|
|||
Well, I probably shouldn't have written "whole function"... let's say the whole block of code where the performance is of interest.
Sure, you may do basic parameter checking in C, allocate the local variables... and then use the inline asm to do the real job. So, you don't have to worry about stack allocation, you have access to fields of C structures... and x64 asm is basically the same as x86, so I wouldn't call it difficult ![]() |
The Following User Says Thank You to gigaman For This Useful Post: | ||
Indigo (07-19-2019) |
![]() |
Thread Tools | |
Display Modes | |
|
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
inline patche | hp3 | Source Code | 3 | 06-04-2021 14:48 |
How to inline x64 asm in vs2017 ? | Mahmoudnia | General Discussion | 25 | 07-22-2018 01:04 |
Inline Patching | MaRKuS-DJM | General Discussion | 1 | 01-24-2004 23:03 |