In C/C++, there is a keyword that allows you to enter assembly language directly
ID: 646925 • Letter: I
Question
In C/C++, there is a keyword that allows you to enter assembly language directly into a method.
Example
int Main()
{
__asm // notify the compiler that this block is assembly language.
{
push ax;
xor ax, ax;
int 33h;
mov ax, 1;
int 33h;
pop ax;
retf;
}
return 0;
}
Believe it or not, my memory has this little snippet of assembly language imprinted for life, or at least until I become senile! It's a little old-school DOS command that calls the mouse interrupt and shows the mouse cursor. That aside, depending on the C/C++ compiler in question, this would be compiled to run on execution of the Main() method.
A few years on, I am a C# and JavaScript developer...can you guess where I'm going with this?
In ASP.NET, We can develop the server-side functionality of a web application with C# (or other .NET languages), and the client-side functionality with JavaScript.
Sometimes we need to be able to pass JavaScript scripts to the client, from the server, because the server is going to provide some information which will be required by the script. Currently these need to be written as strings, which can cause a headache when it comes to debugging!
Example
string js = "function test() { alert("" + message + ""); }";
aspLiteral.Text = ScriptWrapper.Wrap(js); //assume this just wraps a script into a <script> tag.
What if we could enter JavaScript directly into the server-side code, thus giving more exposure to the debugger, intellisense, code-completion etc.
Example
DateTime x = DateTime.Now;
string message = "Hello World";
var script = javascript //special keyword notifies the compiler that this block is JavaScript.
{
function test() {
alert(@message); //Tell the compiler to evaluate C# message to JS message
var x = @x; //Tell the compiler to evaluate C# x to JS x
}
}
aspLiteral.Content = ScriptWrapper.Wrap(script);
// output
<script type="text/javascript">
function test() {
alert("Hello World");
var x = /Date(1371561853440)/;
}
</script>
I know this seems like a monumental mountain to climb, possibly to the point of insanity...Am I the only one who thinks this could be useful, or, does its, somewhat limited use, just not warrant the implementation of this into future compilers / IDEs?
Explanation / Answer
You're clearly not the only one who finds this useful, as it is done all over the place as you've shown above. From putting compiler optimized code into methods for speed, to using scripting languages for their simplicity, people have been co-mingling code for quite a while. The real trick here though, is getting the compiler to know what the heck you're trying to do.
Most compilers are made to interpret 1 language, that is the one they are designed for. The reason that the ASM keyword works so well in C is that ASM is an intermediary step for the C compiler, that is it puts all the C code it finds into ASM equivalents, so when you specify with the __asm keyword, you're saying "Hey compiler, don't worry about turning this into assembly, I've done that for you!" and the compiler goes on its merry way inserting your premade code wherever it needs it before converting it to binary.
When you're doing things like assembling javascript inside of java, that gets more complicated. Why? Because javascript is NOT an intermediary step from java to binary. This means that not only does the compiler have to be able to read java, it has to be able to read javascript as well! On top of that, it has to know how to integrate the two of them when their only common step is binary! As you can see, this gets a little more tricky. That's often the reason that the javascript is put into strings, as it is often passed off to another method that will interpret the javascript and then turn that into java, which the compiler then recognizes and compiles.
So to sum it up, if you want to be able to do this, every compiler would need to be able to successful interpret N languages, where N is the number of different languages you would use in each source file. It'll work for 1 or 2 languages, but when you start getting into 3+ you're looking at exponential code growth. What happens if language 2 is nested in language 3 which is nested in language 1? How does the compiler interpret that? It seems simple on a case by case basis, but coming up with some pre-defined algorithm to automatically sort those kinds of problems out is no small task.