IL 汇编学习笔记(一)

摘要:
今天我开始了IL汇编语言的学习,下面都是对一篇文章学习过程的翻译和摘要。c.通过.maxstackn的语句可以指定其最大容量例子:简单的加法//AddTwoNumbers.assemblyexternmscorlib{}.assemblyAdd{.ver1:0:1:0}.moduleadd.exe.methodstaticvoidmain()cilmanaged{.maxstack2.entrypointldstr"Thesumof50and30is="callvoid[mscorlib]System.Console::Writeldc.i4.s50//加载单字节的整数ldc.i430//加载4个字节的整数addcallvoid[mscorlib]System.Console::Writeret}类型列表:ILName.NETBaseTypeMeaningCLSCompliantVoidnodata,onlyusedasreturntypeNoBoolSystem.BooleanBooleanValueNoCharSystem.CharCharacterValueNoint8System.SByteSingleByteIntegerNoint16System.Int16TwoByteIntegerNoint32System.Int32FourByteIntegerYesint64System.648ByteIntegerYesnativeintSystem.IntPtrSignedIntegerYesunsignedint8System.ByteOnebyteintegerNounsignedint16System.UInt16TwobyteintegerNounsignedint32System.UInt32FourbyteintegerNounsignedint64System.UInt64EightbyteintegerYesnativeunsignedintSystem.UIntPtrUnsignedIntegerYesFloat32System.SingleFourbyteFloatingPointNoFloat64System.DoubleEightbyteFloatingPointNoobjectSystem.ObjectObjecttypevalueYes&ManagedPointerYes*System.IntPtrUnmanagedPointerYestypedefSystem.TypedReferenceSpecialtypethatholdsdataandexplicitlyindicatesthetypeofdata.YesArraySystem.ArrayArrayYesstringSystem.StringStringtypeYes除此之外还有一些类型助记符:.i4,.i4.s,.u4等。例如如下代码:callint32SomeFunction其参数分别是相当于C#的string,int,double.返回一个int.这个例子中用到的都是一些CLS兼容的类型。
今天我开始了 IL 汇编语言的学习,下面都是对一篇文章学习过程的翻译和摘要。
原文:http://www.codeproject.com/dotnet/ilassembly.asp
先从一个 HelloWorld 例子开始:
//Test.IL
//Asimpleprogrammewhichprintsastringontheconsole
.assembly
externmscorlib{}
.assemblyTest
{
.ver
1:0:1:0
}
.moduletest.exe
.method
staticvoidmain()cilmanaged//"cilmanaged"让编译器编译为托管代码{
.maxstack
1//要加载到evaluationstack中的条目数.entrypoint
ldstr
"IamfromtheILAssemblyLanguageIL 汇编学习笔记(一)第1张"//加载字符串到evaluationstack
call
void[mscorlib]System.Console::WriteLine(string)//使用上一个语句加载到evaluationstack里的那个string.ret
}

Evaluation Stack
:评估堆栈
a. 该堆栈用于在方法执行之前,存储所需变量的值。
b. 方法执行后,会自动被清空,或存储一个结果。
c. 通过 .maxstack n 的语句可以指定其最大容量(item 数)
例子:简单的加法
//AddTwoNumbers
.assembly
externmscorlib{}
.assemblyAdd
{
.ver
1:0:1:0
}
.moduleadd.exe
.method
staticvoidmain()cilmanaged
{
.maxstack
2
.entrypoint
ldstr
"Thesumof50and30is="
call
void[mscorlib]System.Console::Write(string)
ldc.i4.s
50//加载单字节的整数ldc.i430//加载4个字节的整数add
call
void[mscorlib]System.Console::Write(int32)
ret
}

类型列表(注意其中的类型是否为 CLS 兼容的):

IL Name

.NET Base Type

Meaning

CLS Compliant

Void

no data, only used as return type

No

Bool

System.Boolean

Boolean Value

No

Char

System.Char

Character Value (16 bit unicode)

No

int8

System.SByte

Single Byte Integer (signed)

No

int16

System.Int16

Two Byte Integer(signed)

No

int32

System.Int32

Four Byte Integer(signed)

Yes

int64

System.64

8 Byte Integer(signed)

Yes

native int

System.IntPtr

Signed Integer

Yes

unsigned int8

System.Byte

One byte integer (unsigned)

No

unsigned int16

System.UInt16

Two byte integer (unsigned)

No

unsigned int32

System.UInt32

Fourbyte integer (unsigned)

No


unsigned int64

System.UInt64

Eightbyte integer (unsigned)

Yes

native unsigned int

System.UIntPtr

Unsigned Integer

Yes

Float32

System.Single

Four byte Floating Point

No

Float64

System.Double

Eightbyte Floating Point

No

object

System.Object

Object type value

Yes

&

Managed Pointer

Yes

*

System.IntPtr

Unmanaged Pointer

Yes

typedef

System.Typed Reference

Special type that holds data and explicitlyindicates the type of data.

Yes

Array

System.Array

Array

Yes

string

System.String

String type

Yes


除此之外还有一些类型助记符:.i4, .i4.s, .u4 等。
例如如下代码:
callint32SomeFunction(string,int32,float64<codelang=msil>)

其参数分别是相当于 C# 的 string, int, double. 返回一个 int.
这个例子中用到的都是一些 CLS 兼容的类型。如果是自定义的类型呢?看下面的例子:
//InC#ColorTranslator.FromHtml(htmlColor)
//InILAsmcallinstancestring[System.Drawing]
System.Drawing.ColorTranslator::ToHtml(valuetype
[System.Drawing]System.Drawing.Color)

注意这里的 valuetype 关键字用于指示一个不是基础数据类型的类型。
如何声明变量
可以用 .locals 指令。
例子及解析:
.localsinit(int32,string)//声明两个变量,一个int一个string.ldc.i434//加载整数34到EvaluationStackstloc.0//保存到变量0(变量从0开始按索引存取,这里就是上面分配的整形变量)ldstr"SomeTextforLocalVariable"//加载字符串stloc.1//赋值给string变量(在位置1)ldloc.0//加载变量0(整数)的值到EvaluationStack.callvoid[mscorlib]System.Console::WriteLine(int32)//打印整数ldloc.1//加载变量1(字符串)的值到EvaluationStack.callvoid[mscorlib]System.Console::WriteLine(string)//打印字符串

以上没有指定变量名称,如果要指定也是可以的:
.localsinit(int32x,int32y)

要加载这种变量,可以用 stloc x 和 stloc y 语句。
(To be continued)

免责声明:文章转载自《IL 汇编学习笔记(一)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇网站目录,文件夹命名规范[转]Unix sar 命令下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

JavaSE基础之double数据类型的格式化

JavaSE基础之double数据类型的格式化 1、double 数据类型的格式化工具类:DoubleFormatUtil.java 1 package cn.com.zfc.util; 2 3 import java.math.BigDecimal; 4 import java.text.DecimalFormat; 5 import jav...

【JavaWeb学习】过滤器Filter

一、简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 html 文件等进行拦截,从而实现一些特殊的功能。例如实现URL级别的权限访问控制、过滤敏感词汇、压缩响应信息等一些高级功能。 Servlet API中提...

bootstrap-datepicker限定可选时间范围

此项目是bootstrap-datetimepicker 项目的一个分支,原项目不支持 Time选择。其它部分也进行了改进、增强,例如 load过程增加了对 ISO-8601 日期格式的支持。文档是拷贝/粘贴字原项目的文档,并且加入了更多细节说明。 别犹豫了,下载下来试试吧 ! 下载 ZIP 包 此地址可以克隆或fork本项目git clone gi...

图像转pdf(c#版)

using iTextSharp.text;using iTextSharp.text.pdf;using iTextSharp.text.pdf.codec;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Wind...

尚硅谷《谷粒商城项目总结》

1、前言 花了几天的时间把尚硅谷的视频项目看完了,跟着做了一遍,基本上没啥大的问题,有几个小问题也做了总结。 技术方面除了 vue/nacos 没用过,其他的基本都用过,我们公司实际开发中用的也就是这一套东西。 中间的不想看,可以直接点击目录,看总结,总结里有你针对此项目所有的总结及问题解决的说明 1.1技术栈 springcloud 统一配置中心:apo...

Servlet第四篇【request对象常用方法、应用】

什么是HttpServletRequest HttpServletRequest**对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,**HTTP请求头中的所有信息都封装在这个对象中,开发人员通过这个对象的方法,可以获得客户这些信息。 简单来说,要得到浏览器信息,就找HttpServletRequest对象 HttpServletRequest...