InvalidProgramException in file .IL (Intermediate Language of Microsoft)

1

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 this be? I am learning MSIL and this is one of my first examples that I try and the truth is that I have no idea why it can not walk.

    
asked by Augusto Herbel 31.07.2016 в 02:11
source

2 answers

1

I already found the solution. The values in the stack were not in order at the time of calling the System.Console.WriteLine method, first a object was passed and then a string , instead of a string and then a object . Create two local variables and with those local variables I put the parameters in order.
I leave the correct code to see what were the errors:

.assembly extern mscorlib {}
.assembly MyFirstSample {}

.method static public void main(class System.String[]) cil managed
{
  .entrypoint
  .maxstack 2
  .locals init (string message,
                string argument)

  ldstr "Hi my name is {0}"
  stloc message

  ldarg.0
  ldc.i4.0
  ldelem.ref
  stloc argument

  ldloc message
  ldloc argument

  call void [mscorlib]System.Console::WriteLine(string, object)
  ret
}
    
answered by 04.08.2016 / 02:01
source
0

I do not think that IL is so thought to learn, it is ams I doubt there are manuals to learn it, maybe if it is used as a means to analyze

You could use tools such as

link

where an .exe or dll disasembler is applied to analyze it in both IL and .net code

you could create your program in c #, apply ILSpy and compare with the code that you are creating to see what differences you note and so infer that you are missing

I think that is a good technique to learn, write code in a language known as C # and see that IL generates

    
answered by 31.07.2016 в 21:08