This is the code
.assembly extern mscorlib {}
.assembly UsingTheArgs {}
.method static public void main() cil managed
{
.entrypoint
.maxstack 1
ldstr "Hi my name is {0}"
ldarg.0
ldc.i4.0
ldelem.ref
call void [mscorlib]System.Console::WriteLine(string, object)
ret
}
And it generates a System.InvalidProgramException
, it tells me:
Common Language Runtime detected an invalid program in main()
What can be caused this? I'm learning MSIL and this is one of my first examples that I try and I really have no idea why it can't work.
I already found the solution. The values on the stack were not in order when the method was called
System.Console.WriteLine
, a was passed firstobject
and then astring
, instead of astring
and then aobject
. I created two local variables and with those local variables I put in order the parameters.I leave the correct code so you can see what the errors were:
I do not think that IL is so designed to be learned, it is even more doubtful that there are manuals to learn it, perhaps if it is used as a means to analyze
You could use tools like being
http://ilspy.net/
where it applies a disassembler of .exe or dll to be able to analyze it both in IL and in .net code
you could create your program in c#, apply ILSpy and compare with the code you are creating to see what differences you notice and thus deduce what you are missing
I think that's a good technique to learn, write code in a known language like C# and see what IL generates