EXETOOLS FORUM  

Go Back   EXETOOLS FORUM > General > x64 OS

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 04-08-2010, 07:28
Fyyre's Avatar
Fyyre Fyyre is offline
VIP
 
Join Date: Dec 2009
Location: 0xfffffffe
Posts: 115
Rept. Given: 38
Rept. Rcvd 57 Times in 26 Posts
Thanks Given: 9
Thanks Rcvd at 50 Times in 11 Posts
Fyyre Reputation: 57
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
Reply With Quote
  #2  
Old 04-08-2010, 17:13
winndy winndy is offline
VIP
 
Join Date: Sep 2005
Posts: 215
Rept. Given: 102
Rept. Rcvd 25 Times in 11 Posts
Thanks Given: 6
Thanks Rcvd at 2 Times in 2 Posts
winndy Reputation: 25
where can I download "Intel C/C++ compiler add-on for Visual Studio "?
Reply With Quote
  #3  
Old 04-08-2010, 17:59
Av0id Av0id is offline
VIP
 
Join Date: Jan 2006
Posts: 399
Rept. Given: 112
Rept. Rcvd 111 Times in 69 Posts
Thanks Given: 0
Thanks Rcvd at 3 Times in 3 Posts
Av0id Reputation: 100-199 Av0id Reputation: 100-199
on vdown for example
Reply With Quote
The Following User Gave Reputation+1 to Av0id For This Useful Post:
winndy (04-09-2010)
  #4  
Old 04-08-2010, 22:44
Fyyre's Avatar
Fyyre Fyyre is offline
VIP
 
Join Date: Dec 2009
Location: 0xfffffffe
Posts: 115
Rept. Given: 38
Rept. Rcvd 57 Times in 26 Posts
Thanks Given: 9
Thanks Rcvd at 50 Times in 11 Posts
Fyyre Reputation: 57
Quote:
Originally Posted by winndy View Post
where can I download "Intel C/C++ compiler add-on for Visual Studio "?
I recommend downloading it straight from Intel's site http://software.intel.com/en-us/arti...uation-center/ then use any TBE Intel.lic to activate it during the installation ;) Can PM me if you need one.

-Fyyre
Reply With Quote
The Following User Gave Reputation+1 to Fyyre For This Useful Post:
winndy (04-09-2010)
  #5  
Old 04-08-2010, 23:10
jump jump is offline
VIP
 
Join Date: Jan 2009
Posts: 274
Rept. Given: 83
Rept. Rcvd 48 Times in 24 Posts
Thanks Given: 9
Thanks Rcvd at 10 Times in 5 Posts
jump Reputation: 49
Quote:
Originally Posted by Fyyre View Post
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
Very interesting news. I have read everywhere that inline assmbler isnt possible on x64 architecture.
Could you show us correct syntax for inline assembler which will Intel C++ Compiler accept ?

--
Jump
Reply With Quote
  #6  
Old 04-09-2010, 00:43
Archer's Avatar
Archer Archer is offline
retired
 
Join Date: Aug 2005
Posts: 195
Rept. Given: 1
Rept. Rcvd 12 Times in 9 Posts
Thanks Given: 0
Thanks Rcvd at 71 Times in 9 Posts
Archer Reputation: 12
Inline asm is forbidden in studio compiler only. Guess they are too lazy to implement it. GCC and intel support it just fine.
Reply With Quote
  #7  
Old 04-09-2010, 10:56
Fyyre's Avatar
Fyyre Fyyre is offline
VIP
 
Join Date: Dec 2009
Location: 0xfffffffe
Posts: 115
Rept. Given: 38
Rept. Rcvd 57 Times in 26 Posts
Thanks Given: 9
Thanks Rcvd at 50 Times in 11 Posts
Fyyre Reputation: 57
Quote:
Originally Posted by jump View Post
Very interesting news. I have read everywhere that inline assmbler isnt possible on x64 architecture.
Could you show us correct syntax for inline assembler which will Intel C++ Compiler accept ?

--
Jump
It is virtually the same as MSVC:

__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
	}
}
Reply With Quote
The Following 2 Users Gave Reputation+1 to Fyyre For This Useful Post:
jump (04-09-2010)
  #8  
Old 04-09-2010, 16:08
jump jump is offline
VIP
 
Join Date: Jan 2009
Posts: 274
Rept. Given: 83
Rept. Rcvd 48 Times in 24 Posts
Thanks Given: 9
Thanks Rcvd at 10 Times in 5 Posts
jump Reputation: 49
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
Reply With Quote
  #9  
Old 04-09-2010, 18:45
Git's Avatar
Git Git is offline
Old Git
 
Join Date: Mar 2002
Location: Torino
Posts: 1,033
Rept. Given: 217
Rept. Rcvd 265 Times in 157 Posts
Thanks Given: 46
Thanks Rcvd at 60 Times in 32 Posts
Git Reputation: 200-299 Git Reputation: 200-299 Git Reputation: 200-299
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
Reply With Quote
  #10  
Old 04-09-2010, 20:39
Av0id Av0id is offline
VIP
 
Join Date: Jan 2006
Posts: 399
Rept. Given: 112
Rept. Rcvd 111 Times in 69 Posts
Thanks Given: 0
Thanks Rcvd at 3 Times in 3 Posts
Av0id Reputation: 100-199 Av0id Reputation: 100-199
Quote:
So if i understand correct, the inline assmbler have to be also x64 not x86,right?
intel compiler supports both x86 and x64 assembler code
Reply With Quote
  #11  
Old 04-11-2010, 15:32
Fyyre's Avatar
Fyyre Fyyre is offline
VIP
 
Join Date: Dec 2009
Location: 0xfffffffe
Posts: 115
Rept. Given: 38
Rept. Rcvd 57 Times in 26 Posts
Thanks Given: 9
Thanks Rcvd at 50 Times in 11 Posts
Fyyre Reputation: 57
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

Quote:
Originally Posted by jump View Post
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
Reply With Quote
  #12  
Old 04-11-2010, 18:38
Git's Avatar
Git Git is offline
Old Git
 
Join Date: Mar 2002
Location: Torino
Posts: 1,033
Rept. Given: 217
Rept. Rcvd 265 Times in 157 Posts
Thanks Given: 46
Thanks Rcvd at 60 Times in 32 Posts
Git Reputation: 200-299 Git Reputation: 200-299 Git Reputation: 200-299
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
Reply With Quote
The Following User Gave Reputation+1 to Git For This Useful Post:
  #13  
Old 04-17-2010, 17:13
gigaman gigaman is offline
Friend
 
Join Date: Jun 2002
Posts: 76
Rept. Given: 0
Rept. Rcvd 3 Times in 2 Posts
Thanks Given: 0
Thanks Rcvd at 3 Times in 3 Posts
gigaman Reputation: 3
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...
Reply With Quote
  #14  
Old 04-17-2010, 18:18
Git's Avatar
Git Git is offline
Old Git
 
Join Date: Mar 2002
Location: Torino
Posts: 1,033
Rept. Given: 217
Rept. Rcvd 265 Times in 157 Posts
Thanks Given: 46
Thanks Rcvd at 60 Times in 32 Posts
Git Reputation: 200-299 Git Reputation: 200-299 Git Reputation: 200-299
I agree gigaman. It even more true when you consider the difficulty in writing a true x64 full function properly.

Git
Reply With Quote
  #15  
Old 04-21-2010, 04:37
gigaman gigaman is offline
Friend
 
Join Date: Jun 2002
Posts: 76
Rept. Given: 0
Rept. Rcvd 3 Times in 2 Posts
Thanks Given: 0
Thanks Rcvd at 3 Times in 3 Posts
gigaman Reputation: 3
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
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inline Patching MaRKuS-DJM General Discussion 1 01-24-2004 23:03
Inline patching a packed dll drasd_20002 General Discussion 20 05-23-2003 15:11


All times are GMT +8. The time now is 07:55.


ICP05004977
vBulletin Security provided by vBSecurity v2.2.0 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX