IL汇编赋值

.assembly extern mscorlib {}
 
 .assembly Test
 {
     .ver 1:0:1:0
 }
 .module test.exe
  
 .method static void main() cil managed
 {
     .maxstack 5
     .entrypoint
     
    .locals init (int32, string)
    ldc.i4 34
    stloc.0
    ldstr "Some Text for Local Variable"
    stloc.1
    ldloc.0
    call void [mscorlib]System.Console::WriteLine(int32)
    ldloc.1
    call void [mscorlib]System.Console::WriteLine(string)
   
     ret
 }

运行如下;

IL汇编赋值_第1张图片

首先是定义了2个局部变量,一个是int32类型,一个是string类型;

指令的含义,

    ldstr string—把一个字符串常量装入堆栈;

    ldc.i4.n—把一个 32 位的常量( n 从 0 到 8)装入堆栈;
    stloc.n— 把一个从堆栈中返回的值存入第 n( n 从 0 到 8)个局部变量;

执行了,

    ldc.i4 34
    stloc.0

    这两句以后,把34赋给定义的第一个整型变量;

执行了,

    ldstr "Some Text for Local Variable"
    stloc.1

    这两句以后,把"Some Text for Local Variable"赋值给定义的第二个string类型变量;

然后就是输出了;

你可能感兴趣的:(.Net,汇编语言,汇编,c#,stloc)