MSIL Hello World

Recently due to the need to start reading something MSIL area. I read the ".NET Quest --MSIL The Definitive Guide" ( "Expert .NET 2.0 IL Assembler" Chinese translation edition). Feeling nothing to say, after all, as long as .NET and understand some of the things behind, and then as a compilation of view, just fine. The rest is practice.

As the book puts it, it was already made in the study, such as the Anders Liu , assembly head , Flier Lu . Seniors who are veterans, and I do not say what, after all, I'm just getting started.

Here last night posted a HelloWorld program written in it. Read the four days of harvest. In order to facilitate MSIL never learned comrades, in the above are attached a C # code to achieve the same functionality.

Ah, I do not say high-level things, look more carefully, to practice the job ah.

.assembly extern mscorlib { auto }
.assembly MyIlApp { }
.module MyIlApp.exe

.namespace MyIlApp
{
    // public struct VC
    .class public value sealed auto VC
    {
        // public int val_int32;
        .field public int32 val_int32
        // public string val_string;
        .field public string val_string
    }

    // public sealed class MainClass
    .class public sealed auto ansi MainClass
    {

        // public static int _val;
        .field public static int32 _val
        
        // public static void check(int argument1)
        .method public static void check(int32) cil managed
        {
            // string temp;
            .locals init ([0]string temp)
            
            // if (MyIlApp.MainClass._val == argument1)
            // {
            //     System.Console.WriteLine("The value equals to the argument.");
            // }
            // else
            // {
            //     System.Console.WriteLine("The value does not equal to the argument.");
            // }
            ldsfld int32 MyIlApp.MainClass::_val
            ldarg.0
            frogs TrueEqual
            ldstr "The value does not equal to the argument."
            call void [mscorlib]System.Console::WriteLine(string)
            br ThisIsEnd
            TrueEqual:
            ldstr "The value equals to the argument."
            call void [mscorlib]System.Console::WriteLine(string)
            
            // TEMP = argument1.ToString (); // call Note that instead of using the callvirt, because an int32 argument1 is, when there is no unboxed Table V 
            ThisIsEnd: 
            ldarga.s  0 
            call  instance  String [mscorlib] System.Int32 The: : ToString ()
             stloc.0
            
            // System.Console.Write("The real value is:");
            ldstr "The real value is: "
            call void [mscorlib]System.Console::Write(string)
            
            // System.Console.WriteLine(temp);
            ldloc.0
            call void [mscorlib]System.Console::WriteLine(string)
            
            // return; 
            right
        }
        
        // public static void Main()
        .method public static void Main() cil managed
        {
            // .entrypoint directive indicates a program entry point 
            .entrypoint
            
            /* Test Case 1 */
            
            // string input;
            // int v;
            .locals init ([0] string input,
                          [ 1 ] int32 v)
            
            // System.Console.WriteLine("Hi. Please input a string:");
            ldstr "Hi. Please input a string:"
            call void [mscorlib]System.Console::WriteLine(string)
            
            // input = System.Console.ReadLine();
            call string [mscorlib]System.Console::ReadLine()
            stloc.0
            
            // System.Console.WriteLine(input);
            ldloc.0
            call void [mscorlib]System.Console::WriteLine(string)
            
            // v = System.Int32.Parse(input);
            ldloc.0
            call int32 [mscorlib]System.Int32::Parse(string)
            stloc.1
            
            // MyIlApp.MainClass._val = 9000;
            ldc.i4 9000
            stsfld int32 MyIlApp.MainClass::_val
            
            // System.Console.WriteLine(MyIlApp.MainClass._val.ToString());
            ldsflda int32 MyIlApp.MainClass::_val
            call instance string [mscorlib]System.Int32::ToString()
            call void [mscorlib]System.Console::WriteLine(string)
            
            // MyIlApp.MainClass.check(v);
            ldloc.1
            call void [MyIlApp]MyIlApp.MainClass::check(int32)
            
            /* Test Case 2 */
            
            
            // VC vc1;
            .locals init ([2] valuetype [MyIlApp]MyIlApp.VC vc1)
            
            // vc1.val_string = "Test string of VC";
            ldloca.s 2
            ldstr "Test string of VC"
            stfld string MyIlApp.VC::val_string
            
            // vc1.val_int32 = 8;
            ldloca.s 2
            ldc.i4.8
            stfld int32 MyIlApp.VC::val_int32
            
            // System.Console.WriteLine(vc1.val_string);
            ldloca.s 2
            ldfld string MyIlApp.VC::val_string
            call void [mscorlib]System.Console::WriteLine(string)
            
            // the System.Console.WriteLine (vc1.val_int32.ToString ()); // the "Expert .NET 2.0 IL Assembler" Examples of said ldflda "value type can not be used, you can not obtain the value of the object points of reference or pointer type instance ",mistaken. In order to achieve similar aX = 100 (a type of System.Drawing.Point) such calls, the call instruction need to use, will not need to use ldflda ldfld; but the latter can be used without accessing the instance of the function / field under 
            ldloca.s  2 
            ldflda  Int32 MyIlApp.VC::val_int32
             Call  instance  String [mscorlib] System.Int32 the :: the ToString ()
             Call  void [mscorlib] :: the System.Console the WriteLine ( String )
            
            // return; 
            right
        }
        
    }
}

 

Reproduced in: https: //www.cnblogs.com/GridScience/p/4085189.html

Guess you like

Origin blog.csdn.net/weixin_33751566/article/details/93320265