當(dāng)前位置:
首頁 >
IL 汇编学习笔记(三)
發(fā)布時(shí)間:2025/5/22
34
豆豆
生活随笔
收集整理的這篇文章主要介紹了
IL 汇编学习笔记(三)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
原文:http://www.codeproject.com/dotnet/ilassembly.asp
創(chuàng)建和使用類對象
首先我們定義一個(gè)簡單的類,它只包含一個(gè)方法用于計(jì)算一個(gè)整數(shù)的平方(square),代碼如下:
.assembly?extern?mscorlib?{}
.assembly?MathLib
{
????.ver?1:0:1:0
}
.module?MathLib.dll
.namespace?HangamaHouse
{????
????.class?public?ansi?auto?MathClass?extends?[mscorlib]System.Object
????{????????
????????.method?public?int32?GetSquare(int32)?cil?managed
????????{
????????????.maxstack?3
????????????ldarg.0???//?加載對象的?this?指針到堆棧上
????????????ldarg.1???//?實(shí)例方法的實(shí)際的參數(shù)索引總是從?1?開始
????????????ldarg.1
????????????mul
????????????ret????????
????????}
????}
}
然后用命令 ILAsm MathLib.il /dll 編譯為一個(gè) DLL.
調(diào)用的范例如下,這里創(chuàng)建了第一個(gè) dll 里面的類對象,并調(diào)用其方法。
.assembly?extern?mscorlib?{}
.assembly?extern?MathLib?{.ver?1:0:1:0}
//
//rest?code?here
//
.method?static?void????Main()?cil?managed
{
????.maxstack?2
????.entrypoint
????.locals?init?(valuetype?[MathLib]HangamaHouse.MathClass?mclass)
????????????
????ldloca?mclass??//?加載對象地址
????ldc.i4?5???????//?方法的參數(shù)
????call?instance?int32?[MathLib]HangamaHouse.MathClass::GetSquare(int32)
????ldstr?"The?Square?of?5?Returned?:?"
????call?void?[mscorlib]System.Console::Write(string)
????call?void?[mscorlib]System.Console::WriteLine(int32)
????ret
}
構(gòu)造器和屬性
構(gòu)造器是這樣定義的:
.class?public?MathClass?extends?[mscorlib]System.ValueType
{
????.field?private?int32?mValue
????//?其他代碼。。
????.method?public?specialname?rtspecialname?instance?void?.ctor()?cil?managed
????{
????????ldarg.0?//?加載?this?指針
????????ldc.i4.s?15
????????stfld?int32?HangamaHouse.MathClass::mValue?//?保存到字段
????????ret
????}
????//?其他代碼。。
可以看到,構(gòu)造器實(shí)際上是一個(gè)叫做 .ctor 的特殊方法。以上代碼中使用 specialname 和 rtspecialname 來標(biāo)注它是一個(gè)特殊名稱的方法。
在 IL 中,構(gòu)造器是不會(huì)自動(dòng)被調(diào)用的,你必須手工去調(diào)用它。下面是一個(gè)例子:
.method?public?static?void?Main()?cil?managed
{
????.maxstack?2
????.entrypoint
????.locals?init?(valuetype?[MathLib]HangamaHouse.MathClass?mclass)
????ldloca?mclass
????call?instance?void?[MathLib]HangamaHouse.MathClass::.ctor()
屬性的實(shí)現(xiàn)跟構(gòu)造器類似,實(shí)際上也是方法。代碼如下:
.method??specialname?public?instance?int32?get_Value()?cil?managed
{
????ldarg.0
????ldfld?int32?HangamaHouse.MathClass::mValue
????ret
}
.method?specialname?public?instance?void?set_Value(int32?)?cil?managed
{
????ldarg.0
????ldarg.1
????stfld?int32?HangamaHouse.MathClass::mValue
???
????ret
}???????
//Define?the?property,?Value
.property?int32?Value()
{
????.get?instance?int32?get_Value()
????.set?instance?void?set_Value(int32?)
}
使用屬性其實(shí)就是調(diào)用以上代碼中的 get_Value 和 set_Value 這兩個(gè)方法:
.maxstack?2?.locals
init?(valuetype?[MathLib]HangamaHouse.MathClass?tclass)
ldloca?tclass
ldc.i4?25
call?instance?void?[MathLib]HangamaHouse.MathClass::set_Value(int32)
ldloca?tclass
call?instance?int32?[MathLib]HangamaHouse.MathClass::get_Value()
ldstr?"Propert?Value?Set?to?:?"
call?void?[mscorlib]System.Console::Write(string)
call?void?[mscorlib]System.Console::WriteLine(int32)
(To be continued)
創(chuàng)建和使用類對象
首先我們定義一個(gè)簡單的類,它只包含一個(gè)方法用于計(jì)算一個(gè)整數(shù)的平方(square),代碼如下:
.assembly?extern?mscorlib?{}
.assembly?MathLib
{
????.ver?1:0:1:0
}
.module?MathLib.dll
.namespace?HangamaHouse
{????
????.class?public?ansi?auto?MathClass?extends?[mscorlib]System.Object
????{????????
????????.method?public?int32?GetSquare(int32)?cil?managed
????????{
????????????.maxstack?3
????????????ldarg.0???//?加載對象的?this?指針到堆棧上
????????????ldarg.1???//?實(shí)例方法的實(shí)際的參數(shù)索引總是從?1?開始
????????????ldarg.1
????????????mul
????????????ret????????
????????}
????}
}
然后用命令 ILAsm MathLib.il /dll 編譯為一個(gè) DLL.
調(diào)用的范例如下,這里創(chuàng)建了第一個(gè) dll 里面的類對象,并調(diào)用其方法。
.assembly?extern?mscorlib?{}
.assembly?extern?MathLib?{.ver?1:0:1:0}
//
//rest?code?here
//
.method?static?void????Main()?cil?managed
{
????.maxstack?2
????.entrypoint
????.locals?init?(valuetype?[MathLib]HangamaHouse.MathClass?mclass)
????????????
????ldloca?mclass??//?加載對象地址
????ldc.i4?5???????//?方法的參數(shù)
????call?instance?int32?[MathLib]HangamaHouse.MathClass::GetSquare(int32)
????ldstr?"The?Square?of?5?Returned?:?"
????call?void?[mscorlib]System.Console::Write(string)
????call?void?[mscorlib]System.Console::WriteLine(int32)
????ret
}
構(gòu)造器和屬性
構(gòu)造器是這樣定義的:
.class?public?MathClass?extends?[mscorlib]System.ValueType
{
????.field?private?int32?mValue
????//?其他代碼。。
????.method?public?specialname?rtspecialname?instance?void?.ctor()?cil?managed
????{
????????ldarg.0?//?加載?this?指針
????????ldc.i4.s?15
????????stfld?int32?HangamaHouse.MathClass::mValue?//?保存到字段
????????ret
????}
????//?其他代碼。。
可以看到,構(gòu)造器實(shí)際上是一個(gè)叫做 .ctor 的特殊方法。以上代碼中使用 specialname 和 rtspecialname 來標(biāo)注它是一個(gè)特殊名稱的方法。
在 IL 中,構(gòu)造器是不會(huì)自動(dòng)被調(diào)用的,你必須手工去調(diào)用它。下面是一個(gè)例子:
.method?public?static?void?Main()?cil?managed
{
????.maxstack?2
????.entrypoint
????.locals?init?(valuetype?[MathLib]HangamaHouse.MathClass?mclass)
????ldloca?mclass
????call?instance?void?[MathLib]HangamaHouse.MathClass::.ctor()
屬性的實(shí)現(xiàn)跟構(gòu)造器類似,實(shí)際上也是方法。代碼如下:
.method??specialname?public?instance?int32?get_Value()?cil?managed
{
????ldarg.0
????ldfld?int32?HangamaHouse.MathClass::mValue
????ret
}
.method?specialname?public?instance?void?set_Value(int32?)?cil?managed
{
????ldarg.0
????ldarg.1
????stfld?int32?HangamaHouse.MathClass::mValue
???
????ret
}???????
//Define?the?property,?Value
.property?int32?Value()
{
????.get?instance?int32?get_Value()
????.set?instance?void?set_Value(int32?)
}
使用屬性其實(shí)就是調(diào)用以上代碼中的 get_Value 和 set_Value 這兩個(gè)方法:
.maxstack?2?.locals
init?(valuetype?[MathLib]HangamaHouse.MathClass?tclass)
ldloca?tclass
ldc.i4?25
call?instance?void?[MathLib]HangamaHouse.MathClass::set_Value(int32)
ldloca?tclass
call?instance?int32?[MathLib]HangamaHouse.MathClass::get_Value()
ldstr?"Propert?Value?Set?to?:?"
call?void?[mscorlib]System.Console::Write(string)
call?void?[mscorlib]System.Console::WriteLine(int32)
(To be continued)
總結(jié)
以上是生活随笔為你收集整理的IL 汇编学习笔记(三)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。