C # INLINE-ASM/EMBED x86 assembly

Source: Internet
Author: User

C # can embed assembler in my eyes C # as a middle-upper language is impossible not to

Why is it that I think of the upper-middle language when I place the assembly code? Keeping pointers from C # can see that I know

There are a lot of people who won't believe C # can use assembler code but C # will be troublesome C # can not directly

Inline assembly (INLINE-ASM) is accurate that C # can only be used (AUTO-ASM) to dynamically assemble this technology is not

C # unique language, VB, C + + three languages can be but dynamic assembly I have seen the most is to be

The use of plug-in and remote assembly injection is actually a kind of extension of dynamic assembly technology but it's hard to

Said JIT after compiling the code is through the remote assembly code written to the managed process execution or a

Parasitic in the shell running technology and "memory run" too lazy to discuss these thoughts on the head big.

From there you can see a simple x86/call assembly embedded in C # and called

Perform a look you'll find it's not too hard I wrote a lot of crap on one of my blog posts, which means

This thing is just a language http://blog.csdn.net/u012395622/article/details/46400569.

We know that when the software runs, all the code is put in virtual memory and the executable code is in memory

Memory protection is generally page_execute_read and 32 but after I studied. Net

The executable code should be page_execute_readwirte and 64 if it's p/invoke.

The protection in the Execute DLL is 32 we can not use read-only protection in inline assembly

If we need to use by. NET to delegate to call so must be readable writable if passed Win32

API to call so use 32 can be a little different, I have studied the easy language on the byte set in memory

How much of the memory protection in the end is consistent with C # 4/page_readwrite but why

Easy language can call and C # can not call has always been a confusing thing to me may be

The difference between the managed heap and the unmanaged heap but I'd rather have a great God come out and help me out.

Because the X86 assembly first needs to switch the target platform to x86 so that it does not cause the C # call Assembly

Code error must not omit this step

First you need to define a parameterized delegate with the emphasis on having such a sentence in the Assembly

Call DWORD Ptr[ebp+8]//Call parameter One

[CSharp]View Plaincopy
    1. [Unmanagedfunctionpointer (CALLINGCONVENTION.CDECL)]
    2. Public Delegate IntPtr Callmethod (IntPtr ptr);

Because it is in VC under the inline assembly finally ported to C # General in the VC under the function of the call mode is Cdcel

Moreover, the following is done according to the Cdcel export function format, so you can not use the __stdcall way

[CSharp]View Plaincopy
  1. [STAThread]
  2. static void Main (string[] args)
  3. {
  4. byte[] Buf_asm = {
  5. //Push EBP
  6. //MOV ebp,esp
  7. //Sub esp,0c0h
  8. //Push EBX
  9. //push ESI
  10. //Push EDI
  11. //Lea edi,[ebp-0c0h]
  12. //MOV ecx,30h
  13. //MOV eax,0cccccccch
  14. //Rep stos dword ptr Es:[edi]
  15. 85, 139, 236, 129, 236, 192, 0, 0, 0, 83, 86, 87, 141, 189, 64,
  16. 255, 255, 255, 185, 48, 0, 0, 0, 184, 204, 204, 204, 204, 243, 171,
  17. //Call DWORD Ptr[ebp+8]
  18. 255, 85, 8,
  19. //Pop EDI
  20. //Pop esi
  21. //Pop ebx
  22. //MOV esp,ebp
  23. //Pop EBP
  24. //RET
  25. 95, 94, 91, 139, 229, 93, 195
  26. };
  27. IntPtr ptr_asm = Sethandlecount (buf_asm);
  28. VirtualProtect (Ptr_asm, Buf_asm. Length);
  29. Callmethod Call_method = Marshal.getdelegateforfunctionpointer (Ptr_asm, typeof (Callmethod)) as CallMethod;
  30. Call_method (Marshal.getfunctionpointerfordelegate (new Action (hello_x86)));
  31. }

First write the assembly you need to embed in a byte array format and pass

The Sethandlecount function is used to fetch the address pointer

[CSharp]View Plaincopy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
    1. static void VirtualProtect (IntPtr ptr, int size)
    2. {
    3. int outmemprotect;
    4. if (! VirtualProtect (PTR, size, outmemprotect, out ))
    5. throw New Exception ("Unable to modify memory protection.");
    6. }

The above function is used to modify memory protection simply to allow delegates to interact, including assembly code that can be called inter-

[CSharp]View Plaincopy
    1. static void Hello_x86 ()
    2. {
    3. Console.title = ((new StackFrame ()). GetMethod ()).  Name;
    4. Console.WriteLine ("I was x86 assembly call a test function.");
    5. Console.readkey (false);
    6. }

The function above is a test function that does not make much sense just to show the use of assembly calls

This function then outputs a response to the information used to prompt the function to be written to the memory Assembly called

Dependent external functions

[CSharp]View Plaincopy
  1. [DllImport ("kernel32.dll", CharSet = CharSet.Auto)]
  2. Public static extern IntPtr Sethandlecount (byte[] value);
  3. [DllImport ("kernel32.dll", SetLastError = true)]
  4. Public static extern bool VirtualProtect (IntPtr lpaddress, int. dwsize, int flnewprotect, out  C13>int lpfloldprotect);

namespaces that depend on

[CSharp]View Plaincopy
    1. Using System;
    2. Using System.Runtime.InteropServices;
    3. Using System.Diagnostics;

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C # INLINE-ASM/EMBED x86 assembly

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.