130 likes | 362 Views
MSIL. C# .NET Software Development. MSIL AKA CIL. What all .NET languages compile to Binary Intermediate Language check out: C:Program FilesMicrosoft Visual Studio .NET 2003SDKv1.1Tool Developers Guidedocs Partition II Metadata.doc Partition III CIL.doc StartDocs.htm.
E N D
MSIL C# .NET Software Development
MSIL AKA CIL • What all .NET languages compile to • Binary Intermediate Language • check out: • C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Tool Developers Guide\docs • Partition II Metadata.doc • Partition III CIL.doc • StartDocs.htm
Making MSIL Readable • ILDasm.exe • Disassembles MSIL to MSIL Assembly Language • ILAsm.exe • Assembles MSIL Assembly Language to MSIL
MSIL: A Stack Machine • All operations are executed on the stack • Parameters • Variables • Return values • Expression evaluation • When a command is executed it: • pushes operands (parameters) on the stack • Executes the command or function • The called function pops its own parameters off the stack • The called function pushes it’s return value onto the stack (if needed) • Read the result from the stack (if available)
Onto the Stack... • Value-types are placed directly on the stack • Reference-types place only the reference on the stack • (But you knew that already) • IL Commands: • ld (load: loads a value onto the stack) • st (store: stores a value in a variable and removes it from the stack)
“Hello IL World” • .entrypoint • describes the function as the program entry point • .maxstack 1 • tells ilasm that the stack will only reach a depth of 1 • ldstr • loads a string onto the stack • call • calls the specified function • ret • returns from the function (clearing anything put on the stack)
Loading an Integer • ldc.i4.7 • ldc: load constant • i4: 4-byte integer • 7: value 7 • stloc.0 • Pops the stack into the first local variable • Type has to match initialization type • for the first 4 locals you can say stloc.n (3 > n > 0) • for locals number 4 – 255, use stloc.s n • ldloc.0 • Loads local at index 0 onto the stack
Doing Some Math • ldc.r4 (A4 AA 8A 40) • Loads the hex value as a 4-byte floating point • conv.r4 • converts the top value of the stack to r4 • add • adds the top values of the stack, removes them, pushes the result • sub • subtracts the top values of the stack
Try if-else • bne.un.s IL_000f • branch (jump) if the top two operands are not equal • br.s IL_0012 • branch unconditionally • box • unbox • switch (next demo)
Building Classes • Constructors • stfld • stores a field • Property • newobj • Creates a new object
Exceptions • .try • catch • finally • callvirt • Calls a virtual function (or Property)