Generating asm file..

R

Rajkiran R.B.

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..
 
P

Peter Duniho

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/library/6s2x2bzy(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
 
R

Rajkiran R.B.

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.Collections.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.WriteLine("\nEnter Valid Options");
Console.WriteLine("\nOptions:\ne - encoding\nd -
decoding");
flag = 1;
}
if (File.Exists(srcfile) == false)
{
Console.WriteLine("\nSource File Not Present.");
flag = 1;
}
if (flag == 0)
{
doProcess();
}

}
else
{
Console.WriteLine("\nInvalid Parameters\n");
Console.WriteLine("\nSyntax:\n\nfbase64.exe [options]
[Source File Name] [Destination Filename]");
Console.WriteLine("\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(srcfile, 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.ToBase64String(src);
//code to write the encoded data
FileStream fd = new FileStream(destfile, FileMode.Create,
FileAccess.Write);
StreamWriter sr = new StreamWriter(fd);
sr.Write(dest);
sr.Close();
fd.Close();
Console.WriteLine("\nEncoding Complete");
}
catch (Exception ex)
{
Console.WriteLine("Error While Encoding File\n" +
ex.Message);
System.Environment.Exit(0);
}
}


public static void doDecoding()
{
try
{
//Code To Read The Source File into a string
FileStream fs = new FileStream(srcfile, 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.FromBase64String(src);
//code to write the decoded information into the destination
file
FileStream fd = new FileStream(destfile, FileMode.Create,
FileAccess.Write);
fd.Write(dest, 0, dest.Length);
fd.Close();
Console.WriteLine("\nDecoding Complete");
}
catch (Exception ex)
{
Console.WriteLine("Error While Decoding File\n" +
ex.Message);
System.Environment.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..
 
L

Liz

Rajkiran R.B. said:
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.Collections.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.WriteLine("\nEnter Valid Options");
Console.WriteLine("\nOptions:\ne - encoding\nd -
decoding");
flag = 1;
}
if (File.Exists(srcfile) == false)
{
Console.WriteLine("\nSource File Not Present.");
flag = 1;
}
if (flag == 0)
{
doProcess();
}

}
else
{
Console.WriteLine("\nInvalid Parameters\n");
Console.WriteLine("\nSyntax:\n\nfbase64.exe [options]
[Source File Name] [Destination Filename]");
Console.WriteLine("\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(srcfile, 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.ToBase64String(src);
//code to write the encoded data
FileStream fd = new FileStream(destfile, FileMode.Create,
FileAccess.Write);
StreamWriter sr = new StreamWriter(fd);
sr.Write(dest);
sr.Close();
fd.Close();
Console.WriteLine("\nEncoding Complete");
}
catch (Exception ex)
{
Console.WriteLine("Error While Encoding File\n" +
ex.Message);
System.Environment.Exit(0);
}
}


public static void doDecoding()
{
try
{
//Code To Read The Source File into a string
FileStream fs = new FileStream(srcfile, 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.FromBase64String(src);
//code to write the decoded information into the
destination file
FileStream fd = new FileStream(destfile, FileMode.Create,
FileAccess.Write);
fd.Write(dest, 0, dest.Length);
fd.Close();
Console.WriteLine("\nDecoding Complete");
}
catch (Exception ex)
{
Console.WriteLine("Error While Decoding File\n" +
ex.Message);
System.Environment.Exit(0);
}
}
}
}
 
J

Jon Skeet [C# MVP]

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
 
R

Rajkiran R.B.

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..
 
R

Rajkiran R.B.

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
 
A

Alberto Poblacion

Rajkiran R.B. said:
[...]
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.ToBase64String(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.ToBase64String(...)), so the assembly language generated in this way
would not contain the code for performing the base 64 encoding.
 
J

Jon Skeet [C# MVP]

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
 
R

Rajkiran R.B.

Well the operating system is Ms dos 6.22

Jon Skeet said:
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
 
P

Peter Duniho

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
 
J

Jon Skeet [C# MVP]

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
 
A

Arne Vajhøj

Rajkiran said:
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:

http://www.vajhoej.dk/arne/eksperten/b64/b64.h
http://www.vajhoej.dk/arne/eksperten/b64/b64.cpp

(just remove all the DLL related stuff)

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top