Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Well let's say I have this bunch of code :
Console.WriteLine("Hello World");

What I want my program (in C#) to do is translate this code to :
mov si,mystring
call print_string
mystring db 'Hello World'

Is there any simple way to do that? (If not then complex ways are also okay)
Any help would be appreciated.....
~Sids123
Posted

1 solution

C# does not compile directly to native code - it compiles to an Intermediate Language which is later converted to the native instructions by the JIT compiler.

If you what to view the IL, that's one thing - and it's easy enough to do - but you can't look at the native code because it isn't stored with the source.

If you actually want process the string "Console.WriteLine("Hello World");" in your application and convert it to executable instructions, then you basically have to write a C# compiler, and provide pretty much the whole of .NET (Or at least a large percentage of it) in order to support the code you generate - and even print_string isn't a trivial exercise! It's a lot of work, but you will find many, many books on compiler writing - Google will help there.
 
Share this answer
 
Comments
sid2x 17-Aug-13 6:46am    
Thanks OriginalGriff, But I am not writing a compiler,There are just 5-6 commands that I want to
translate like that, really sorry if I didn't specify that...
OriginalGriff 17-Aug-13 7:05am    
It doesn't matter if you want to "translate" 5 or six command s- you still need to do a fair amount of the same work, unless you do a brute-force-and-ignorance approach and just look explicitly in the string for the fixed strings - but that can get to be a nightmare if you actually parse code looking for the stuff, because code does not have to be "neat and tidy" and exactly the way you expect.

What are you actually trying to achieve with this approach? There may be a better way.
sid2x 17-Aug-13 7:07am    
So any recommendations to start with?......
OriginalGriff 17-Aug-13 7:11am    
Depends I guess on what you are trying to achieve...why are you trying to do this?
sid2x 17-Aug-13 7:18am    
To be honest I am coding an Operating System in Assembly, So far it can read and write files, run binary files
that are compiled by NASM, It now has 5 system calls one of them is print_string I am trying to do this because it would be easy for me to build applications. That's the truth. I could write this thing in Assembly but I am trying to do this in a high level language like C#.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900