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