Exetools  

Go Back   Exetools > General > General Discussion

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 06-17-2018, 08:22
Mahmoudnia's Avatar
Mahmoudnia Mahmoudnia is offline
Family
 
Join Date: Nov 2012
Location: Iran
Posts: 192
Rept. Given: 55
Rept. Rcvd 137 Times in 45 Posts
Thanks Given: 87
Thanks Rcvd at 151 Times in 75 Posts
Mahmoudnia Reputation: 100-199 Mahmoudnia Reputation: 100-199
How to inline x64 asm in vs2017 ?

Hi
Can I use inline x64 asm in vs 2017 ?
When I use inline asm in x64 , this error show up :

nonstandard extension used: '__asm' keyword not supported on this architecture
__________________
All about software security references
https://t.me/securebyte

Last edited by Mahmoudnia; 06-17-2018 at 15:36.
Reply With Quote
  #2  
Old 06-17-2018, 09:53
deepzero's Avatar
deepzero deepzero is offline
VIP
 
Join Date: Mar 2010
Location: Europe
Posts: 218
Rept. Given: 99
Rept. Rcvd 60 Times in 38 Posts
Thanks Given: 83
Thanks Rcvd at 95 Times in 50 Posts
deepzero Reputation: 60
There is no x64 inline assembly with the Microsoft compiler.

Quote:
One of the constraints for the x64 compiler is to have no inline assembler support. This means that functions that cannot be written in C or C++ will either have to be written as subroutines or as intrinsic functions supported by the compiler. Certain functions are performance sensitive while others are not. Performance-sensitive functions should be implemented as intrinsic functions.
Reply With Quote
The Following 2 Users Say Thank You to deepzero For This Useful Post:
Mahmoudnia (06-17-2018), tonyweb (06-18-2018)
  #3  
Old 06-17-2018, 14:31
user1's Avatar
user1 user1 is offline
Family
 
Join Date: Sep 2012
Location: Romania
Posts: 757
Rept. Given: 397
Rept. Rcvd 110 Times in 58 Posts
Thanks Given: 447
Thanks Rcvd at 412 Times in 246 Posts
user1 Reputation: 31
http://masm32.com/board/index.php?topic=4211.0

maybe useful in check options.
Reply With Quote
The Following 2 Users Say Thank You to user1 For This Useful Post:
Mahmoudnia (06-17-2018), tonyweb (06-18-2018)
  #4  
Old 06-18-2018, 02:16
Archer's Avatar
Archer Archer is offline
retired
 
Join Date: Aug 2005
Posts: 224
Rept. Given: 1
Rept. Rcvd 30 Times in 18 Posts
Thanks Given: 2
Thanks Rcvd at 343 Times in 49 Posts
Archer Reputation: 30
You have several options.
1. Switch to some other compiler like intel or GCC. You can still use Visual Studio, just a different compiler, they don't have inline asm restrictions.
2. Compile a separate .asm and link with other compiled .cpp files. This can be configured, so it's done automatically when the solution is built.
3. Sometimes it's enough to use intrinsics. But of course they don't cover all asm instructions.
Reply With Quote
The Following 2 Users Say Thank You to Archer For This Useful Post:
Mahmoudnia (06-18-2018), tonyweb (06-18-2018)
  #5  
Old 06-18-2018, 02:22
Mahmoudnia's Avatar
Mahmoudnia Mahmoudnia is offline
Family
 
Join Date: Nov 2012
Location: Iran
Posts: 192
Rept. Given: 55
Rept. Rcvd 137 Times in 45 Posts
Thanks Given: 87
Thanks Rcvd at 151 Times in 75 Posts
Mahmoudnia Reputation: 100-199 Mahmoudnia Reputation: 100-199
@Archer
I am trying to use GCC x64
__________________
All about software security references
https://t.me/securebyte
Reply With Quote
  #6  
Old 06-18-2018, 03:08
chants chants is offline
Family
 
Join Date: Jul 2016
Posts: 456
Rept. Given: 2
Rept. Rcvd 30 Times in 18 Posts
Thanks Given: 373
Thanks Rcvd at 723 Times in 331 Posts
chants Reputation: 30
Or as a fourth option since it has yet to be mentioned, write a tool which at compile time extracts all inline code from C modules intended for x64 compilation, put them in an .asm file with some type of label or function definition, compile them, replace the C code with an appropriate control flow transfer, and so forth.

Unfortunately, nothing will be exactly equivalent mentioned so far in MSVC as the control flow transfer is pretty hard to avoid.

Best yet might be to keep requesting MS to make the long overdo change as a developer feedback or feature request.
Reply With Quote
  #7  
Old 06-18-2018, 14:54
Evilcry Evilcry is offline
Friend
 
Join Date: Jan 2009
Posts: 55
Rept. Given: 4
Rept. Rcvd 15 Times in 9 Posts
Thanks Given: 1
Thanks Rcvd at 38 Times in 16 Posts
Evilcry Reputation: 15
Two ways:

- Intrinsics https://msdn.microsoft.com/en-us/library/26td21ds.aspx
- As above suggested, .asm linking here a tutorial on how to setup VS + MASM

http://lallouslab.net/2016/01/11/introduction-to-writing-x64-assembly-in-visual-studio/

Best Regards,
Evilcry
Reply With Quote
The Following 2 Users Say Thank You to Evilcry For This Useful Post:
Mahmoudnia (06-18-2018), tonyweb (06-18-2018)
  #8  
Old 06-18-2018, 19:35
chants chants is offline
Family
 
Join Date: Jul 2016
Posts: 456
Rept. Given: 2
Rept. Rcvd 30 Times in 18 Posts
Thanks Given: 373
Thanks Rcvd at 723 Times in 331 Posts
chants Reputation: 30
But since you are not linking the .asm inline, there are excessive call or jump statements emitted. The best would be if MS were to add it.
Reply With Quote
  #9  
Old 06-20-2018, 01:34
gigaman gigaman is offline
Friend
 
Join Date: Jun 2002
Posts: 85
Rept. Given: 0
Rept. Rcvd 3 Times in 2 Posts
Thanks Given: 0
Thanks Rcvd at 13 Times in 10 Posts
gigaman Reputation: 4
Does one call or jump really matter?
I mean, if you said you couldn't easily access local variables or structures, I'd agree... but "excessive call", it sounds like you are trying to optimize something. In that case inline assembler is hardly any good - it's a blackbox for the compiler (at least for the Microsoft's) so it has to dump the values from registers into local variables and after the inline assembly load them back. In other words, a piece of inline assembly heavily breaks the optimization of the surrounding C code - so it's usually not worth it, it does more damage than one call would (so it's better to write the whole CPU intensive piece of code in assembler as a separate function).
Reply With Quote
The Following User Says Thank You to gigaman For This Useful Post:
tonyweb (06-20-2018)
  #10  
Old 06-20-2018, 05:38
chants chants is offline
Family
 
Join Date: Jul 2016
Posts: 456
Rept. Given: 2
Rept. Rcvd 30 Times in 18 Posts
Thanks Given: 373
Thanks Rcvd at 723 Times in 331 Posts
chants Reputation: 30
It matters because its very convenient to program like this. By having to call/return or jump/jump or what have you (also don't forget all the stack setup and cleanup), it forces calling conventions and requires the parameters to be dealt with and such. Yes the MS implementation is not as clever as in GCC/GAS where you can really customize details of the behavior. I agree for optimization its a lame point as you would better be off with pure asm or optimized C rather than a mix and match without sophisticated inline-ing support.

Further its easier to write portable 64/32 bit code without calling conventions and clever use of macros, as the calling conventions are so different you have to use different assembly instructions (registers vs stack).

A C function can modify itself in memory also using clever tricks with inline assembler which has its obfuscation or other uses.

But I suppose this discussion is easily already documented:
Quote:
https://msdn.microsoft.com/en-us/library/80ccffx3.aspx
Quote:
Advantages of Inline Assembly
Because the inline assembler doesn't require separate assembly and link steps, it is more convenient than a separate assembler. Inline assembly code can use any C variable or function name that is in scope, so it is easy to integrate it with your program's C code. Because the assembly code can be mixed inline with C or C++ statements, it can do tasks that are cumbersome or impossible in C or C++.
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.
Writing prolog and epilog code for "naked" calls.
So optimization is on the list after all
Reply With Quote
The Following User Says Thank You to chants For This Useful Post:
tonyweb (06-20-2018)
  #11  
Old 06-21-2018, 01:44
atom0s's Avatar
atom0s atom0s is offline
Family
 
Join Date: Jan 2015
Location: 127.0.0.1
Posts: 247
Rept. Given: 24
Rept. Rcvd 101 Times in 47 Posts
Thanks Given: 42
Thanks Rcvd at 419 Times in 165 Posts
atom0s Reputation: 100-199 atom0s Reputation: 100-199
While you can't use inline asm, you can link ASM files into your program and use a separate compiler such as MASM to build .asm files with your project. Visual Studio has support for this built-in.

If you absolutely need inline asm you can use a different compiler/linker.
Reply With Quote
  #12  
Old 06-21-2018, 21:11
chants chants is offline
Family
 
Join Date: Jul 2016
Posts: 456
Rept. Given: 2
Rept. Rcvd 30 Times in 18 Posts
Thanks Given: 373
Thanks Rcvd at 723 Times in 331 Posts
chants Reputation: 30
Well it looks like it will not happen anytime either. Unless we all get together to vote it to the top. Difficult to reason about proving correctness in the compiler I suppose

Quote:
https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/suggestions/2609085-support-inline-assembler-on-c-64-bit
Quote:
DECLINED·

Admin
Visual Studio Team (Product Team, Microsoft Visual Studio) responded · May 11, 2016
Because of our experience based implementing x86 inline assembler and the many correctness issues we’ve faced with it, we don’t recommend that developers use this approach and won’t be implementing this for new architectures.
As a workaround, you can use the Microsoft Assembler for x64 (https://msdn.microsoft.com/en-us/library/hb5z4sxd.aspx) to create an .OBJ file that you can link against.
-C++ Team
Reply With Quote
The Following User Says Thank You to chants For This Useful Post:
Newbie_Cracker (07-11-2018)
  #13  
Old 07-17-2018, 04:00
Avalon Avalon is offline
Friend
 
Join Date: Jul 2018
Posts: 7
Rept. Given: 0
Rept. Rcvd 0 Times in 0 Posts
Thanks Given: 1
Thanks Rcvd at 9 Times in 7 Posts
Avalon Reputation: 0
Just create a .ASM file, change the build rule to MASM, define the subroutine and call it from the C file.


masm.asm
Quote:
.CODE

PUBLIC MyAsmRoutine
PUBLIC ChangeRaxRoutine

MyAsmRoutine PROC
push rbp
mov rbp, rsp
call qword ptr [rcx]
mov rsp, rbp
pop rbp
ret
MyAsmRoutine ENDP

ChangeRaxRoutine PROC
mov rax, 0x4141
ChangeRcxRoutine ENDP

END
file.c
Quote:
void MyAsmRoutine(PVOID pFunc);
void __declspec(naked) ChangeRaxRoutine();

int main()
{
PVOID pNtDirectCall = ....
MyAsmRoutine(pNtDirectCall);
ChangeRaxRoutine();
//now your program will return 0x4141 as RAX is the return code
}

Last edited by Avalon; 07-17-2018 at 04:05.
Reply With Quote
The Following User Says Thank You to Avalon For This Useful Post:
niculaita (07-17-2018)
  #14  
Old 07-17-2018, 06:56
Insid3Code's Avatar
Insid3Code Insid3Code is offline
Family
 
Join Date: May 2013
Location: Algeria
Posts: 80
Rept. Given: 44
Rept. Rcvd 60 Times in 30 Posts
Thanks Given: 23
Thanks Rcvd at 99 Times in 53 Posts
Insid3Code Reputation: 60
@avalon
Typo...

ChangeRaxRoutine PROC
mov rax, 0x4141
ChangeRcxRoutine ENDP

....
.code

public ChangeRaxRoutine

ChangeRaxRoutine proc
mov rax, 04141h
ChangeRaxRoutine endp
end
__________________
Computer Forensics
Reply With Quote
The Following User Says Thank You to Insid3Code For This Useful Post:
niculaita (07-17-2018)
  #15  
Old 07-18-2018, 19:23
vic4key vic4key is offline
Friend
 
Join Date: Apr 2010
Posts: 29
Rept. Given: 5
Rept. Rcvd 8 Times in 4 Posts
Thanks Given: 34
Thanks Rcvd at 7 Times in 5 Posts
vic4key Reputation: 8
Just an example, hope can help you: https://github.com/vic4key/MS-Mix-Cpp-n-Asm-in-64-bit
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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
X64 inline asm Fyyre x64 OS 48 08-10-2014 16:50
Inline Patching MaRKuS-DJM General Discussion 1 01-24-2004 23:03


All times are GMT +8. The time now is 01:34.


��ICP��05004977��
Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX