Generating asm file..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rajkiran R.B.

    Generating asm file..

    I have written a program in C#...
    Now the requirement is that I want an assembly code to be the output rather
    than the regular executable output..
    Can anyone say how to do it..




  • Jeff Louie

    #2
    Re: Generating asm file..

    Rajkiran... I believe you are asking how to look at the generated Common
    Intermediate Language (CIL) as assembly opcodes? So if you want to see
    ldc.i4 instead of 0x20 you can run the ildasm or il dis-assembler.


    Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


    Regards,
    Jeff

    *** Sent via Developersdex http://www.developersdex.com ***

    Comment

    • Peter Duniho

      #3
      Re: Generating asm file..

      On Tue, 04 Dec 2007 22:45:13 -0800, Rajkiran R.B.
      <rajkiranpro@ho tmail.comwrote:
      I have written a program in C#...
      Now the requirement is that I want an assembly code to be the output
      rather than the regular executable output..
      Can anyone say how to do it..
      It depends on what you mean by "assembly code" and "output". :)

      Your C# program wouldn't be represented as x86 assembly, since it gets
      compiled to the Microsoft Intermediate Language (MSIL). I don't know how
      to get VS to generate that directly, but you can always run your compiled
      C# objects or executables through the ildasm.exe tool.

      I don't see any command line switch for the C# compiler
      (http://msdn2.microsoft.com/en-us/lib...zy(VS.90).aspx) that
      provides for the output of alternative types of files, like .asm or
      pre-processed .cs (though, because of the nature of C# and .NET, I suppose
      those types of files aren't quite as useful or necessary as they would be
      with C/C++ or similar).

      If you want x86 assembly, I don't know of a straightforward way to do
      that. Your code isn't going to be x86 until it's actually running and has
      been compiled by the "just-in-time" compiler. I'd guess there's a way to
      somehow inspect the resulting compiled code in memory, output that
      somehow, and run it through a disassembler. But a) that is likely a lot
      more trouble than what you're looking for, and b) I don't know off the top
      of my head how you'd go about doing that. :)

      Pete

      Comment

      • Rajkiran R.B.

        #4
        Re: Generating asm file..

        Thanks for your replies...


        Let me put in my exact requirement..

        I need a program that would read a file and either encrypt it or decrypt it
        using the base 64 algorithm.. For that purpose I used C# to code it.. Here
        is the code I wrote..



        using System;
        using System.Collecti ons.Generic;
        using System.Text;
        using System.IO;

        namespace fbase64
        {
        class fbase64
        {
        static string srcfile, destfile, operation;


        static void Main(string[] args)
        {
        if (args.Length == 3)
        {
        int flag = 0;
        operation = args[0];
        srcfile = args[1];
        destfile = args[2];
        if(operation!=" e" && operation!="d")
        {
        Console.WriteLi ne("\nEnter Valid Options");
        Console.WriteLi ne("\nOptions:\ ne - encoding\nd -
        decoding");
        flag = 1;
        }
        if (File.Exists(sr cfile) == false)
        {
        Console.WriteLi ne("\nSource File Not Present.");
        flag = 1;
        }
        if (flag == 0)
        {
        doProcess();
        }

        }
        else
        {
        Console.WriteLi ne("\nInvalid Parameters\n");
        Console.WriteLi ne("\nSyntax:\n \nfbase64.exe [options]
        [Source File Name] [Destination Filename]");
        Console.WriteLi ne("\nOptions:\ ne - encoding\nd - decoding");
        }

        }

        public static void doProcess()
        {

        if (operation == "e")
        {
        doEncoding();
        }
        else
        {
        doDecoding();
        }

        }

        public static void doEncoding()
        {
        try
        {
        //Code To Read The Source File into a byte array
        FileStream fs = new FileStream(srcf ile, FileMode.Open,
        FileAccess.Read );
        byte[] src = new byte[fs.Length];
        fs.Read(src, 0, Convert.ToInt32 (fs.Length));
        fs.Close();
        //Code to encode the read data
        string dest = Convert.ToBase6 4String(src);
        //code to write the encoded data
        FileStream fd = new FileStream(dest file, FileMode.Create ,
        FileAccess.Writ e);
        StreamWriter sr = new StreamWriter(fd );
        sr.Write(dest);
        sr.Close();
        fd.Close();
        Console.WriteLi ne("\nEncoding Complete");
        }
        catch (Exception ex)
        {
        Console.WriteLi ne("Error While Encoding File\n" +
        ex.Message);
        System.Environm ent.Exit(0);
        }
        }


        public static void doDecoding()
        {
        try
        {
        //Code To Read The Source File into a string
        FileStream fs = new FileStream(srcf ile, FileMode.Open,
        FileAccess.Read );
        StreamReader sd = new StreamReader(fs );
        string src = sd.ReadToEnd();
        sd.Close();
        fs.Close();
        //Code to decode The file
        byte[] dest = Convert.FromBas e64String(src);
        //code to write the decoded information into the destination
        file
        FileStream fd = new FileStream(dest file, FileMode.Create ,
        FileAccess.Writ e);
        fd.Write(dest, 0, dest.Length);
        fd.Close();
        Console.WriteLi ne("\nDecoding Complete");
        }
        catch (Exception ex)
        {
        Console.WriteLi ne("Error While Decoding File\n" +
        ex.Message);
        System.Environm ent.Exit(0);
        }
        }
        }
        }



        Now I need the same thing to be done using an assembly language program.. So
        is it possible for me to convert the program to an unmanaged c++ code so
        that I can generate an x86 assembly file using the gcc compiler..

        Comment

        • Liz

          #5
          Re: Generating asm file..


          "Rajkiran R.B." <rajkiranpro@ho tmail.comwrote in message
          news:7D01144E-CDD8-4108-B085-5B1CCBD669D7@mi crosoft.com...
          Thanks for your replies...
          >
          >
          Let me put in my exact requirement..
          >
          I need a program that would read a file and either encrypt it or decrypt
          it using the base 64 algorithm.. For that purpose I used C# to code it..
          Here is the code I wrote..
          >
          .....
          Now I need the same thing to be done using an assembly language program..
          So is it possible for me to convert the program to an unmanaged c++ code
          so that I can generate an x86 assembly file using the gcc compiler..
          of course you can convert this to c++ ... you can re-write it; but I
          wouldn't call that "an assembly language program"





          >
          using System;
          using System.Collecti ons.Generic;
          using System.Text;
          using System.IO;
          >
          namespace fbase64
          {
          class fbase64
          {
          static string srcfile, destfile, operation;
          >
          >
          static void Main(string[] args)
          {
          if (args.Length == 3)
          {
          int flag = 0;
          operation = args[0];
          srcfile = args[1];
          destfile = args[2];
          if(operation!=" e" && operation!="d")
          {
          Console.WriteLi ne("\nEnter Valid Options");
          Console.WriteLi ne("\nOptions:\ ne - encoding\nd -
          decoding");
          flag = 1;
          }
          if (File.Exists(sr cfile) == false)
          {
          Console.WriteLi ne("\nSource File Not Present.");
          flag = 1;
          }
          if (flag == 0)
          {
          doProcess();
          }
          >
          }
          else
          {
          Console.WriteLi ne("\nInvalid Parameters\n");
          Console.WriteLi ne("\nSyntax:\n \nfbase64.exe [options]
          [Source File Name] [Destination Filename]");
          Console.WriteLi ne("\nOptions:\ ne - encoding\nd -
          decoding");
          }
          >
          }
          >
          public static void doProcess()
          {
          >
          if (operation == "e")
          {
          doEncoding();
          }
          else
          {
          doDecoding();
          }
          >
          }
          >
          public static void doEncoding()
          {
          try
          {
          //Code To Read The Source File into a byte array
          FileStream fs = new FileStream(srcf ile, FileMode.Open,
          FileAccess.Read );
          byte[] src = new byte[fs.Length];
          fs.Read(src, 0, Convert.ToInt32 (fs.Length));
          fs.Close();
          //Code to encode the read data
          string dest = Convert.ToBase6 4String(src);
          //code to write the encoded data
          FileStream fd = new FileStream(dest file, FileMode.Create ,
          FileAccess.Writ e);
          StreamWriter sr = new StreamWriter(fd );
          sr.Write(dest);
          sr.Close();
          fd.Close();
          Console.WriteLi ne("\nEncoding Complete");
          }
          catch (Exception ex)
          {
          Console.WriteLi ne("Error While Encoding File\n" +
          ex.Message);
          System.Environm ent.Exit(0);
          }
          }
          >
          >
          public static void doDecoding()
          {
          try
          {
          //Code To Read The Source File into a string
          FileStream fs = new FileStream(srcf ile, FileMode.Open,
          FileAccess.Read );
          StreamReader sd = new StreamReader(fs );
          string src = sd.ReadToEnd();
          sd.Close();
          fs.Close();
          //Code to decode The file
          byte[] dest = Convert.FromBas e64String(src);
          //code to write the decoded information into the
          destination file
          FileStream fd = new FileStream(dest file, FileMode.Create ,
          FileAccess.Writ e);
          fd.Write(dest, 0, dest.Length);
          fd.Close();
          Console.WriteLi ne("\nDecoding Complete");
          }
          catch (Exception ex)
          {
          Console.WriteLi ne("Error While Decoding File\n" +
          ex.Message);
          System.Environm ent.Exit(0);
          }
          }
          }
          }
          >
          >
          >

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Generating asm file..

            On Dec 5, 7:43 am, "Rajkiran R.B." <rajkiran...@ho tmail.comwrote:

            <snip>
            Now I need the same thing to be done using an assembly language program.. So
            is it possible for me to convert the program to an unmanaged c++ code so
            that I can generate an x86 assembly file using the gcc compiler..
            Given that you won't be able to use .NET from the unmanaged code
            (without hosting the CLR, which pretty much defeats the point), you
            basically need to rewrite from scratch using the unmanaged crypto
            APIs.

            Why do you have this requirement anyway?

            Jon

            Comment

            • Rajkiran R.B.

              #7
              Re: Generating asm file..

              Yes I know that it wouldn't be an assembly language program...
              But there is a compiler option in gcc compiler to generate an equivalent
              assembly file from the code..
              Im familiar with C# but not with c++..
              so is there a code converter to do that..
              or can anyone provide me with the code snippets so that I can build the c++
              code which does the same job as this..

              of course you can convert this to c++ ... you can re-write it; but I
              wouldn't call that "an assembly language program"

              Comment

              • Rajkiran R.B.

                #8
                Re: Generating asm file..

                Well this base64 algorithm needs to be implemented in an electronic project
                and it uses a microprocessor. . so I need to burn the program in a chip..
                for that I need the assembly program...
                And this program is also to be implemented in an older machine which uses
                ..386 processor

                "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                news:0f00b4b8-5326-4681-9d2d-8f2aa1f30831@i1 2g2000prf.googl egroups.com...
                On Dec 5, 7:43 am, "Rajkiran R.B." <rajkiran...@ho tmail.comwrote:
                >
                <snip>
                >
                >Now I need the same thing to be done using an assembly language program..
                >So
                >is it possible for me to convert the program to an unmanaged c++ code so
                >that I can generate an x86 assembly file using the gcc compiler..
                >
                Given that you won't be able to use .NET from the unmanaged code
                (without hosting the CLR, which pretty much defeats the point), you
                basically need to rewrite from scratch using the unmanaged crypto
                APIs.
                >
                Why do you have this requirement anyway?
                >
                Jon
                >

                Comment

                • Alberto Poblacion

                  #9
                  Re: Generating asm file..

                  "Rajkiran R.B." <rajkiranpro@ho tmail.comwrote in message
                  news:7D01144E-CDD8-4108-B085-5B1CCBD669D7@mi crosoft.com...
                  [...]
                  I need a program that would read a file and either encrypt it or decrypt
                  it using the base 64 algorithm.
                  Note that the base 64 algorithm does NOT _encrypt_, it merely _encodes_
                  the file. Anyone will be able to decode it back, since it doesn't use any
                  kind of cryptography.
                  For that purpose I used C# to code it.
                  [...]
                  string dest = Convert.ToBase6 4String(src);
                  [...] Now I need the same thing to be done using an assembly language
                  program. So is it possible for me to convert the program to an unmanaged
                  c++ code so that I can generate an x86 assembly file using the gcc
                  compiler.
                  Even if you could turn your own code into assembly, the encoding itself
                  is done by a library in the .Net Framework (the call to
                  Convert.ToBase6 4String(...)), so the assembly language generated in this way
                  would not contain the code for performing the base 64 encoding.

                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: Generating asm file..

                    On Dec 5, 8:56 am, "Rajkiran R.B." <rajkiran...@ho tmail.comwrote:
                    Well this base64 algorithm needs to be implemented in an electronic project
                    and it uses a microprocessor. . so I need to burn the program in a chip..
                    for that I need the assembly program...
                    And this program is also to be implemented in an older machine which uses
                    .386 processor
                    And what operating system is this using? There may well be a base 64
                    routine in whatever platform you're targeting, but without saying what
                    the platform actually is, there's no way of knowing.

                    Base64 isn't too hard to implement from scratch, but your C# code is
                    going to be no use to you at all, except to verify results.

                    Jon

                    Comment

                    • Rajkiran R.B.

                      #11
                      Re: Generating asm file..

                      Well the operating system is Ms dos 6.22

                      "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                      news:ce93f190-5f82-4387-818b-3a818b5bee18@o4 2g2000hsc.googl egroups.com...
                      On Dec 5, 8:56 am, "Rajkiran R.B." <rajkiran...@ho tmail.comwrote:
                      >Well this base64 algorithm needs to be implemented in an electronic
                      >project
                      >and it uses a microprocessor. . so I need to burn the program in a chip..
                      >for that I need the assembly program...
                      >And this program is also to be implemented in an older machine which uses
                      >.386 processor
                      >
                      And what operating system is this using? There may well be a base 64
                      routine in whatever platform you're targeting, but without saying what
                      the platform actually is, there's no way of knowing.
                      >
                      Base64 isn't too hard to implement from scratch, but your C# code is
                      going to be no use to you at all, except to verify results.
                      >
                      Jon

                      Comment

                      • Peter Duniho

                        #12
                        Re: Generating asm file..

                        On Wed, 05 Dec 2007 00:56:18 -0800, Rajkiran R.B.
                        <rajkiranpro@ho tmail.comwrote:
                        Well this base64 algorithm needs to be implemented in an electronic
                        project and it uses a microprocessor. . so I need to burn the program in
                        a chip.. for that I need the assembly program...
                        And this program is also to be implemented in an older machine which
                        uses .386 processor
                        Well, here's the thing. The code you posted, all of the interesting stuff
                        is implemented in .NET. So unless you've got a way to run the .NET
                        framework on your electronic project, it doesn't matter _what_ you compile
                        that code to, it's not going to work.

                        Base64 isn't all that hard to implement yourself. It barely qualifies as
                        encryption, IMHO. Sort of like ROT-13 is encryption.

                        The basic functionality you're trying to implement would be relatively
                        easy even in C++. So that's certainly an option. I would guess that a
                        little Googling would probably even turn up a handful or so of existing
                        implementations for Base64 in C or C++.

                        Not that you asked, but it seems to me that you have the additional
                        problem of what other services are available on the eletronic project.
                        The code you posted not only does Base64 encoding, it also assumes a
                        console with standard input and output, and some sort of file i/o API.
                        The standard CRT includes these things, but it's not clear from your post
                        that you could even use the standard CRT on your electronic project.

                        Basically, it seems pretty clear that whatever you're trying to do, .NET
                        isn't going to help you. You can't write .NET code to implement
                        functionality unless you can run the resulting executable in an
                        environment where .NET is supported. It doesn't sound like that's the
                        case here.

                        Pete

                        Comment

                        • Jon Skeet [C# MVP]

                          #13
                          Re: Generating asm file..

                          On Dec 5, 9:10 am, "Rajkiran R.B." <rajkiran...@ho tmail.comwrote:
                          Well the operating system is Ms dos 6.22
                          Okay. You need to find a C or C++ Base64 implementation then. I'm sure
                          there are plenty on the NET.

                          Just as a side-note, it would have been *much* more useful to explain
                          all of this to start with.

                          Jon

                          Comment

                          • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                            #14
                            Re: Generating asm file..

                            Rajkiran R.B. wrote:
                            Now I need the same thing to be done using an assembly language
                            program.. So is it possible for me to convert the program to an
                            unmanaged c++ code so that I can generate an x86 assembly file using the
                            gcc compiler..
                            As many other have stated: no, because you can not use
                            ..NET IO or Base64 functions.

                            You open your C++ IDE with an empty file and start coding.

                            I happen to have some Base64 code on the shelf:




                            (just remove all the DLL related stuff)

                            Arne

                            Comment

                            Working...