IL 彙編學習筆記(三)

2021-09-05 14:45:22 字數 1657 閱讀 3144

原文:

建立和使用類物件

首先我們定義乙個簡單的類,它只包含乙個方法用於計算乙個整數的平方(square),**如下:

.assembly 

extern

mscorlib {}

.assembly mathlib

.module mathlib.dll

.namespace

hangamahouse}}

然後用命令 ilasm mathlib.il /dll 編譯為乙個 dll.

呼叫的範例如下,這裡建立了第乙個 dll 裡面的類物件,並呼叫其方法。

.assembly 

extern

mscorlib {}

.assembly 

extern

mathlib 

////

rest code here

//.method 

static

void

main() cil managed

構造器和屬性

構造器是這樣定義的: .

class

public

mathclass extends [mscorlib]system.valuetype

//其他**。。

可以看到,構造器實際上是乙個叫做 .ctor 的特殊方法。以上**中使用 specialname 和 rtspecialname 來標註它是乙個特殊名稱的方法。

在 il 中,構造器是不會自動被呼叫的,你必須手工去呼叫它。下面是乙個例子:

.method 

public

static

void

main() cil managed

.method specialname 

public

instance 

void

set_value(int32 ) cil managed

//define the property, value

.property int32 value()

使用屬性其實就是呼叫以上**中的 get_value 和 set_value 這兩個方法:

.maxstack 

2.locals

init (valuetype [mathlib]hangamahouse.mathclass tclass)

ldloca tclass

ldc.i4 

25call 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)

IL 彙編學習筆記(三)

原文 http www.codeproject.com dotnet ilassembly.asp 建立和使用類物件 首先我們定義乙個簡單的類,它只包含乙個方法用於計算乙個整數的平方 square 如下 assembly extern mscorlib assembly mathlib module...

IL 彙編學習筆記(四)

原文 建立 windows form 程式 下列 給出乙個簡單的例子,其中簡單的設定了窗體的一些屬性,backcolor,text 和 windowstate.namespace myform 以下是其構造器的定義 method public specialname rtspecialname in...

彙編學習筆記三

我們可以以在一段記憶體中存放資料 資料段 存放我們自己定義的資料 我們可以在一段記憶體中存放指令 段 我們自己定義的指令 我們可以將一段記憶體定義成棧空間 棧段 我們需要存放臨時資料 我們應該怎麼讓cpu按照我們的要求哪 對於資料段 段位址 ds段位址暫存器 0 1 來訪問記憶體單元 對於 段 我們...