Microsofter ildasm.exe

Source: Internet
Author: User
The Microsoft ildasm.exe program analyzes. Net assembly and extracts the Il code from the program as required. After calling this application for an assembly, ildasm will provide a view of all classes and namespaces in the Assembly, as shown in:

Ildasm browse assembly

When you enter a member of a class or its method, ildasm will display the Il code of this Member for you. If you have seen assembler or J ++ bytecode before, il may seem a bit familiar to you. On the other hand, if you only know something about abstract high-level programming languages, then Il looks more like gibberish.

Well, now you know how to look at assembly's il code, but what does this code mean? Before answering this question, let's first learn about CLR.

Virtual CPU
For. net programs,. Net CLR functions like a virtual CPU, which executes il code and operation data. Similar to the real CPU, CLR uses temporary copies of program variables instead of directly operating on variables in the memory. CLR stores these program variables on the stack. The behavior of copying a variable from the memory to the stack is called loading, while the behavior of copying a variable from the stack to the memory is called storage ).

So the process of adding two numbers should be as follows:

1. Load 1st numbers and push them into the stack.

2. Load 2nd numbers and push them into the stack.

3. Extract the two numbers from the stack and add them together.

4. Store the results to the memory.

What is a stack?
The key to understanding Il is to know how the stack works. A stack is an abstract data structure. Its operation mechanism is post-import, first-out. When you push a new entry into the stack, any entry in the stack will be pushed to the depth of the stack. Similarly, removing an entry from the stack moves all other entries in the stack to the top of the stack. Only the top entries of the stack can be retrieved from the stack. The order in which entries exit the stack is the same as that in which they are pushed to the stack. You may think back to the vending machine's loading and picking process.

Important il statements
Now that you understand the basic knowledge of CLR operations, let's discuss the code in front of you. Why? No code is displayed? Take a look at the Il Code listed here.

The first thing you see is the Il Declaration of the current method, including the method name, return type, parameter list, and other modifiers attached to this method (static/shared, public, virtual, etc ). The object constructor is assigned a special name:. ctor.

In Il, method parameters are referenced in sequence based on their positions in the parameter list. If the method is static or shared, the parameter 0 is the 1st parameter in the parameter list. For an instance method, parameter 0 is the pointer (me or this) to the instance of the class where the method is located ). All local variables in the method are declared in the same way in the section marked by. Locals.

After declaring all the local variables, the actual body of the program starts. Each il command or opcode can start with an il _ mark as a code line according to your preferences. Next we will learn more about the more important il commands.

Variable usage
Commands starting with LD load variables from memory to the stack for their operations. There are several load commands, each of which operates on specific types of variables. The following are some of the load commands:

LDC loads a numerical constant into the stack. This command has two modifiers. The first is the type identifier, and the second is the actual value.
Ldloc loads a local variable into the stack. In addition, the ldloca command loads the address of a local variable (rather than the content of the variable) into the stack. Variables are identified by their location in the. Locals section. These commands use different syntax for the 4 and later positions, but the index number will appear in the command.
Ldarg loads a parameter of the member, while the ldarga command loads the parameter address. Variables are identified by their locations in the. Locals section. These commands use different syntax for the Mount position 4 and later, but the index number still appears in the command.
Ldelem loads array elements into the stack and is usually used before other loading statements that indicate the index.
Ldlen loads the length of an array into the stack.
Ldfld and ldsfld load the class fields (member variables) and static class fields into the stack. A domain is identified by a full name.
Each load command has a corresponding storage command, which starts with "St" and stores an entry in the memory. For example, stloc stores the top entry of the stack into a local variable. The syntax rules for storing the specified variables are usually similar to their corresponding loading commands.

Comparison
If you cannot compare two values and make a decision based on the comparison results, many problems cannot be solved in any programming language. Il has a set of comparison operators. They all start with a C Letter and compare the values in the stack. Generally, if the comparison result is true, 1 is pushed into the stack, otherwise 0 is pushed.

Most of these commands are easily distinguished by their names. For example, CEQ compares two values to determine whether they are equal, and CGT determines whether the value at the top of the stack is larger than the second value at the top. The CLT class is the same as that of CGT, but the execution is smaller than the comparison operation.

Goto
Generally, some operations are performed based on the comparison results after the two values are compared. The IL branch command (beginning with Br) jumps to other commands Based on the content in the top entry of the stack. Brtrue and brfalse pop up the top entry of the stack, and then jump to the specified code line based on whether the item is true (1) or false (0. If no command jump is executed, continue to execute the next command. There is also an unconditional branch operator Br, which always jumps to the specified code line.

You will find that the branch operation is like the if statement in the source code and the explicitly executed goto operation. The branch commands in Il also have peers with advanced flow control structures, such as if, case, while, and.

Create new objects and call other code
Call and callvirt commands call other methods and functions. Call usually indicates that the called method is static or shared, while callvirt is used for instance methods. For the two commands, the method names are included in the commands. Any parameters sent to the method will pop up the stack and should be loaded before the method is called.

Because the constructor needs to be called to create a new object, il object creation is similar to other method calls. The parameter is first loaded to the stack, and then runs the newobj command. It calls the object constructor and puts the object index back into the stack. The object name in the command.

The above are general IL syntax operations. In addition to satisfying your desire for knowledge, I hope that you can get enough information from my elaboration to understand the true meaning of the Il code.

(From http://www.dreamsky.cn/blog/article.asp? Id = 233)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.