摘要:今天我开始了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
.assemblyexternmscorlib{}
.assemblyTest
{
.ver1:0:1:0
}
.moduletest.exe
.methodstaticvoidmain()cilmanaged//"cilmanaged"让编译器编译为托管代码{
.maxstack1//要加载到evaluationstack中的条目数.entrypoint
ldstr"IamfromtheILAssemblyLanguage"//加载字符串到evaluationstack
callvoid[mscorlib]System.Console::WriteLine(string)//使用上一个语句加载到evaluationstack里的那个string.ret
}
Evaluation Stack:评估堆栈
a. 该堆栈用于在方法执行之前,存储所需变量的值。
b. 方法执行后,会自动被清空,或存储一个结果。
c. 通过 .maxstack n 的语句可以指定其最大容量(item 数)
例子:简单的加法
//AddTwoNumbers
.assemblyexternmscorlib{}
.assemblyAdd
{
.ver1:0:1:0
}
.moduleadd.exe
.methodstaticvoidmain()cilmanaged
{
.maxstack2
.entrypoint
ldstr"Thesumof50and30is="
callvoid[mscorlib]System.Console::Write(string)
ldc.i4.s50//加载单字节的整数ldc.i430//加载4个字节的整数add
callvoid[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)