Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Write Assembly code in C#

Write Assembly code in C#

Scheduled Pinned Locked Moved C#
csharpquestion
14 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S shahramkeyboard

    how i can use Assembly code in C# ?

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #4

    I suggest you stop trying to write your piece of malware (which is really the only thing it can be, if you want to disable interrupts)

    A 1 Reply Last reply
    0
    • P Pete OHanlon

      You can't directly. .NET uses a form of intermediate language which is intended to be target neutral. If you want to use assembly code, you need to look at writing your application either in assembly, or in C/C++. .NET is entirely the wrong choice here.

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      A Offline
      A Offline
      AspDotNetDev
      wrote on last edited by
      #5

      Yeah, can't be done directly, but I would guess it can be done. Create a C++ project that uses "__asm {...}", reference that project from a managed C++ project, then reference that project from a C# project. Might have to mark some functions as "unsafe" or something, but I would guess something like that would work.

      [Forum Guidelines]

      L 1 Reply Last reply
      0
      • A AspDotNetDev

        Yeah, can't be done directly, but I would guess it can be done. Create a C++ project that uses "__asm {...}", reference that project from a managed C++ project, then reference that project from a C# project. Might have to mark some functions as "unsafe" or something, but I would guess something like that would work.

        [Forum Guidelines]

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #6

        Wouldn't there be a more direct way? Such as doing the __asm part in C++/CLI and merging it with the compiled C# with ILMerge? (would that work?)

        A 1 Reply Last reply
        0
        • S shahramkeyboard

          how i can use Assembly code in C# ?

          S Offline
          S Offline
          snouto
          wrote on last edited by
          #7

          yes it can be done using c++ project wrapper then you would add reference to that dll into .net , but be careful , when you do so , you lose the portability that microsoft struggles for when it creates .NET technology , it can't be run on different OS because your embedded assembly code will be targetted for specific platform . Requirements to create the c++ wrapper dll 1 - Decorate the method in c++ as "extern" to stop the compiler from changing the method name when you compile your DLL . 2 - you can even call the dll methods without using unsafe declaration , because it will be treated like any dll referenced in .NET project. i hope this helps

          Human knowledge belongs to the world.

          1 Reply Last reply
          0
          • S shahramkeyboard

            how i can use Assembly code in C# ?

            H Offline
            H Offline
            Henry Minute
            wrote on last edited by
            #8

            Strictly speaking, you can't as others have said. However this thread[^] says that you can use simple assembler instructions inline. WARNING You have to register to download the code. No personal details are required though. I also doubt that it compiles down to a binary file, don't know because I haven't looked at it myself yet. Still it might be fun even if not actually useful.

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.

            1 Reply Last reply
            0
            • S shahramkeyboard

              how i can use Assembly code in C# ?

              A Offline
              A Offline
              AspDotNetDev
              wrote on last edited by
              #9

              Optimize your C# with Inline ASM via ATL and C++[^]

              [Forum Guidelines]

              1 Reply Last reply
              0
              • L Lost User

                Wouldn't there be a more direct way? Such as doing the __asm part in C++/CLI and merging it with the compiled C# with ILMerge? (would that work?)

                A Offline
                A Offline
                AspDotNetDev
                wrote on last edited by
                #10

                I thought ILMerge only worked for IL assemblies. C++ with assembly code would compile directly to machine code. Would ILMerge still work then? And even if it did work then, I don't understand how that would make things more direct than creating a few projects and having a couple references in Visual Studio.

                [Forum Guidelines]

                L 1 Reply Last reply
                0
                • L Lost User

                  I suggest you stop trying to write your piece of malware (which is really the only thing it can be, if you want to disable interrupts)

                  A Offline
                  A Offline
                  AspDotNetDev
                  wrote on last edited by
                  #11

                  Why in the world do you think the OP is creating malware or is trying to disable interrupts? Did the OP modify the question, because I don't see any mention of disabling interrupts. There are many uses for assembly language in higher level languages, usually relating to performance. For example, at the bottom of my SlimList article, I mention that I can use BSR (an assembly command) to do a quicker base 2 integer log calculation than would be possible using plain C#.

                  [Forum Guidelines]

                  L 1 Reply Last reply
                  0
                  • A AspDotNetDev

                    Why in the world do you think the OP is creating malware or is trying to disable interrupts? Did the OP modify the question, because I don't see any mention of disabling interrupts. There are many uses for assembly language in higher level languages, usually relating to performance. For example, at the bottom of my SlimList article, I mention that I can use BSR (an assembly command) to do a quicker base 2 integer log calculation than would be possible using plain C#.

                    [Forum Guidelines]

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #12

                    Did you miss this[^]? Maybe you where occupied in darker areas? ;P

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    I only read formatted code with indentation, so please use PRE tags for code snippets.


                    I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                    A 1 Reply Last reply
                    0
                    • L Luc Pattyn

                      Did you miss this[^]? Maybe you where occupied in darker areas? ;P

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                      I only read formatted code with indentation, so please use PRE tags for code snippets.


                      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                      A Offline
                      A Offline
                      AspDotNetDev
                      wrote on last edited by
                      #13

                      Luc Pattyn wrote:

                      Did you miss this[^]?

                      Yep.

                      Luc Pattyn wrote:

                      Maybe you where occupied in darker areas?

                      QuickAnswers... a dark place indeed. ;P

                      [Forum Guidelines]

                      1 Reply Last reply
                      0
                      • A AspDotNetDev

                        I thought ILMerge only worked for IL assemblies. C++ with assembly code would compile directly to machine code. Would ILMerge still work then? And even if it did work then, I don't understand how that would make things more direct than creating a few projects and having a couple references in Visual Studio.

                        [Forum Guidelines]

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #14

                        That's why I was planning C++/CLI, it makes mixed assemblies right? At the very least, mixed assemblies exist in the wild, even though I'm not sure how they were made..

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups