MSIL C#.NET Software Development. MSIL AKA CIL What all.NET languages compile to What all.NET...

Post on 20-Jan-2016

216 views 0 download

Transcript of MSIL C#.NET Software Development. MSIL AKA CIL What all.NET languages compile to What all.NET...

MSILMSIL

C# .NETC# .NET

Software DevelopmentSoftware Development

MSIL AKA CILMSIL AKA CIL

What all .NET languages compile toWhat all .NET languages compile to Binary Intermediate LanguageBinary Intermediate Language check out:check out:

C:\Program Files\Microsoft Visual C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Tool Studio .NET 2003\SDK\v1.1\Tool Developers Guide\docsDevelopers Guide\docs Partition II Metadata.docPartition II Metadata.doc Partition III CIL.docPartition III CIL.doc StartDocs.htmStartDocs.htm

Making MSIL ReadableMaking MSIL Readable

ILDasm.exeILDasm.exe Disassembles MSIL to MSIL Assembly Disassembles MSIL to MSIL Assembly

LanguageLanguage ILAsm.exeILAsm.exe

Assembles MSIL Assembly Language to Assembles MSIL Assembly Language to MSILMSIL

MSIL: A Stack MachineMSIL: A Stack Machine All operations are executed on the stackAll operations are executed on the stack

ParametersParameters VariablesVariables Return valuesReturn values Expression evaluationExpression evaluation

When a command is executed it:When a command is executed it: pushes operands (parameters) on the stackpushes operands (parameters) on the stack Executes the command or functionExecutes the command or function

The called function pops its own parameters off the The called function pops its own parameters off the stackstack

The called function pushes it’s return value onto the The called function pushes it’s return value onto the stack (if needed)stack (if needed)

Read the result from the stack (if available)Read the result from the stack (if available)

Onto the Stack...Onto the Stack...

Value-types are placed directly on Value-types are placed directly on the stackthe stack

Reference-types place only the Reference-types place only the reference on the stackreference on the stack (But you knew that already)(But you knew that already)

IL Commands:IL Commands: ldld (load: loads a value onto the stack) (load: loads a value onto the stack) stst (store: stores a value in a variable (store: stores a value in a variable

and removes it from the stack)and removes it from the stack)

““Hello IL World”Hello IL World” .entrypoint.entrypoint

describes the function as the program entry pointdescribes the function as the program entry point .maxstack 1.maxstack 1

tells ilasm that the stack will only reach a depth of tells ilasm that the stack will only reach a depth of 11

ldstrldstr loads a string onto the stackloads a string onto the stack

callcall calls the specified functioncalls the specified function

retret returns from the function (clearing anything put returns from the function (clearing anything put

on the stack)on the stack)

Loading an IntegerLoading an Integer ldc.i4.7ldc.i4.7

ldc: load constantldc: load constant i4: 4-byte integeri4: 4-byte integer 7: value 77: value 7

stloc.0stloc.0 Pops the stack into the first local variablePops the stack into the first local variable Type has to match initialization typeType has to match initialization type for the first 4 locals you can say stloc.n (3 > n for the first 4 locals you can say stloc.n (3 > n

> 0)> 0) for locals number 4 – 255, use stloc.s nfor locals number 4 – 255, use stloc.s n

ldloc.0ldloc.0 Loads local at index 0 onto the stackLoads local at index 0 onto the stack

Doing Some MathDoing Some Math

ldc.r4 (A4 AA 8A 40)ldc.r4 (A4 AA 8A 40) Loads the hex value as a 4-byte floating Loads the hex value as a 4-byte floating

pointpoint conv.r4conv.r4

converts the top value of the stack to r4converts the top value of the stack to r4 addadd

adds the top values of the stack, removes adds the top values of the stack, removes them, pushes the resultthem, pushes the result

subsub subtracts the top values of the stacksubtracts the top values of the stack

Try if-elseTry if-else

bne.un.s IL_000fbne.un.s IL_000f branch (jump) if the top two operands branch (jump) if the top two operands

are not equalare not equal br.s IL_0012br.s IL_0012

branch unconditionallybranch unconditionally boxbox unboxunbox switch (next demo)switch (next demo)

Building ClassesBuilding Classes

ConstructorsConstructors stfldstfld

stores a fieldstores a field PropertyProperty newobjnewobj

Creates a new objectCreates a new object

ExceptionsExceptions

.try.try catchcatch finallyfinally callvirtcallvirt

Calls a virtual function (or Property)Calls a virtual function (or Property)

Talk about GC!Talk about GC!