首页 > 其他分享 >IL阅读第二篇,拆箱装箱

IL阅读第二篇,拆箱装箱

时间:2023-04-03 16:15:31浏览次数:35  
标签:拆箱 string System mscorlib call IL WriteLine 装箱

            //装箱拆箱
            string name = "Zery";
            int age = 22;
            Console.WriteLine(age.ToString() + name);//已ToString的操作
            Console.WriteLine(age+name);//未ToString操作

老规矩,一段C#,一段IL

	// Method begins at RVA 0x2050
	// Code size 48 (0x30)
	.maxstack 2
	.entrypoint
	.locals init (
		[0] string name,
		[1] int32 age
	)

	IL_0000: nop
	IL_0001: ldstr "Zery"
	IL_0006: stloc.0
	IL_0007: ldc.i4.s 22
	IL_0009: stloc.1
	IL_000a: ldloca.s age
	IL_000c: call instance string [mscorlib]System.Int32::ToString()
	IL_0011: ldloc.0
	IL_0012: call string [mscorlib]System.String::Concat(string, string)
	IL_0017: call void [mscorlib]System.Console::WriteLine(string)
	IL_001c: nop
	IL_001d: ldloc.1
	IL_001e: box [mscorlib]System.Int32
	IL_0023: ldloc.0
	IL_0024: call string [mscorlib]System.String::Concat(object, object)
	IL_0029: call void [mscorlib]System.Console::WriteLine(string)
	IL_002e: nop
	IL_002f: ret

 

第一段:
IL_0000: nop以上的之前有过介绍,这次不做介绍
ldstr:推送对元数据中存储的字符串的新对象引用。
stloc.0加载到索引变量的第一位
ldc.i4.s:将位于特定索引处的局部变量的地址加载到计算堆栈上(短格式)。
stloc.1加载到索引变量的第二位
赋值之前一篇也讲过,这里不详细


IL_000a: ldloca.s age
IL_000c: call instance string [mscorlib]System.Int32::ToString()
IL_0011: ldloc.0
第二段:
ldloca.s :将位于特定索引处的局部变量的地址加载到计算堆栈上。
意思是将string类型的age变量,压入计算栈中
然后调用函数ToString,并将结果压入栈中
ldloc.0:将索引 0 处的局部变量加载到计算堆栈上。这样栈上就有两个了


IL_0012: call string [mscorlib]System.String::Concat(string, string)
IL_0017: call void [mscorlib]System.Console::WriteLine(string)
第三段:
栈弹出两个量,然后执行Concat,返回的值压入栈,
再弹出一个量,在执行WriteLine。


IL_001c: nop
IL_001d: ldloc.1
IL_001e: box [mscorlib]System.Int32
IL_0023: ldloc.0
第四段:

Box:将值类转换为对象引用(O 类型)。
装箱box,先将第二个变量值压入计算栈,然后执行box装箱(填出栈,做box操作,返回值压入栈),再压入第一个变量的值


IL_0024: call string [mscorlib]System.String::Concat(object, object)
IL_0029: call void [mscorlib]System.Console::WriteLine(string)
IL_002e: nop
IL_002f: ret
第五段:
栈中弹出两个量执行Concat函数,返回值压入栈
栈中弹出一个量执行WriteLine

 

标签:拆箱,string,System,mscorlib,call,IL,WriteLine,装箱
From: https://www.cnblogs.com/RainbowInTheSky/p/4569144.html

相关文章

  • cmake CMAKE_BUILD_TYPE
    https://stackoverflow.com/questions/24460486/cmake-build-type-is-not-being-used-in-cmakelists-txtTherearetwotypesofgenerators:single-configurationsandmulti-configurations.1.SingleconfigurationsMake-likegenerators:UnixMakefiles,NMakeMakef......
  • Ruby_rails 开发环境配置项 figaro 或者 dotenv
    https://rubygems.org/gems/figaro https://github.com/bkeepers/dotenv figaro用法:bundleexecfigaroinstall然后就会生成config/application.yml#Addconfigurationvalueshere,asshownbelow.##pusher_app_id:"294"#pusher_key:7381a978f7dd7f79a1117#......
  • SyntaxError: Non-UTF-8 code starting with ‘\xb2‘ in file xxx.py but no encodi
    openCV系列文章目录文章目录openCV系列文章目录前言一、问题原因二、解决办法1.点击“运行按钮”->RunPythonfile前言#coding=gbkimportcv2importnumpyasnpdefmouse_callback(event,x,y,flags,userData):print(event,x,y,flags,userData)#mouse_callb......
  • 【已解决】configure: error: C++ compiler cannot create executables
    1.背景 centos7在升级gccconfigure的时候出现的问题A100-01-$build#../configure--prefix=/usr/local/gcc--enable-threads=posix--disable-checking--disable-multilib--enable-languages=c,c++checkingbuildsystemtype...x86_64-pc-linux-gnucheckinghosts......
  • .net6(.net core)使用MailKit收取邮件乱码的问题
    配置IMAP地址后,MailKit读取邮件时中文乱码。主要问题是.netcore以后默认缺失中文字符集。1.安装System.Text.Encoding.CodePages包  2.在Main方法注册staticvoidMain(){//注册字符集,缺失字符集,一些中文编码数据为乱码Encoding......
  • Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump
    日志项目使用Maven的install打包,出现Pleaserefertodumpfiles(ifanyexist)[date].dump,[date]-jvmRun[N].dumpand[date].dumpstream.问题,导致打包失败。原因是Test类不通过,应该是测试代码有问题。解决方式一:选择下图的图标,然后install会跳过测试环节方式二:检......
  • Spring Initailizr(项目初始化向导)
    本地创建官网创建版在Spring官网https://start.spring.io/中选择此时这个项目以压缩包形式下载到本地文件中,然后解压,导入IDEA中阿里start创建如果国外的网址不能通过,将网址更换为http://start.aliyun.com更换版本号在pom.xml中更换maven依赖......
  • stm32------使用keil和标准外设库进行stm32工程模板创建
    keil软件版本:V5.35.00st官网:STM32固件-意法半导体STMicroelectronics单片机型号:STM32F103VET6配置步骤大致为:新建本地文件夹--->新建工程--->添加本地文件到相应工程--->打开魔术棒进行配置--->添加main文件step1:新建本地工程文件夹(这里取名为template,因后续想要做为模板使......
  • 巧用rpmbuild的expand宏实现模板功能
    需求:构建生成的二进制包的个数不确定,由某些条件决定。比如,我们想为系统中的所有内核版本构建某个外来模块,如果系统中有2个内核版本,就生成两个2个二进制包,分别对应相应的版本,如果系统中有3个内核版本,就生成3个二进制包。我们的需求是实现动态个数的%package。很多人在讨论spec是......
  • No supported authentication methods available (server sent: publickey)错误解决
     Nosupportedauthenticationmethodsavailable(serversent:publickey)错误解决 https://blog.csdn.net/cl2227619761/article/details/109840203PasswordAuthenticationyes 实践: #pwd/etc/ssh#vimsshd_config#systemctlrestartsshd  翻译搜索复制......