Looking at one of Il with a hard head: the first date with IL

Source: Internet
Author: User
Tags reflector

When I got back to the computer, everyone else went to the Black shop. I did not catch up.
What are the other things I'm bored?

Recently, I want to write something to listen to events. After reflecting eventinfo, I need a dynamic constructor,
An Opcodes parameter is required. A query of help actually represents a stuff of Il.

Very depressing. So I can see if I can understand it.

Start with the simplest one. This is because the tutorial offered by Microsoft to teach Il is too painful.

I set C #CodeCompile it into a DLL, and then decompile it back with reflector to see if it can be understood.

C # code:
Public void testmethod ()
{
Int A = 3;
Int B = 4;
Int c = A + B;
}
Easy! But it is very depressing.

switch to Il:
. method public hidebysig instance void testmethod () cel managed
{< br>. maxstack 2
. locals Init (
[0] int32 A,
[1] int32 B,
[2] int32 c)
l_0000: NOP
l_0001: LDC. i4.3
l_0002: stloc.0
l_0003: LDC. i4.4
l_0004: stloc.1
l_0005: ldloc.0
l_0006: ldloc.1
l_0007: Add
l_0008: stloc.2
l_0009: RET
}< br> compared with the Opcodes class, it seems that you can understand a little bit.
the previous messy definition is skipped for the moment. It seems to be out of my capacity range.
the first sentence is NOP.
invalid command.. Is that what it means? Maybe how many BITs can increase the execution speed?
pure speculation.
the second sentence is LDC. i4.3
I noticed that LDC. I4 means pushing a number into the stack for computing.
LDC. i4.3 is an overall instruction, which means 3 is pushed into the stack. When I checked the Opcodes definition, I noticed nine commands (0 to 8).

It is used to push a specified value into the stack. Other data must be implemented by adding a value to LDC. I4. It should be because of the ratio of the numbers

Is it common? And 8 is exactly the third power of 2.
The stack used for this computing is very interesting. It seems that most of the operations are tossing this stuff, which reminds me of the pointer when I learned C.

I ran and ran it. I accidentally calculated the result.
Should ld be short for load? Guess. But the c... Unlimited imagination...

The second sentence is stloc.0,
Set local. What does this mean? Place the data at the top of the stack to the No. 0 in the local variable list. That is,. This sentence is:

Bounce the stack top and assign it to. In this way, a needs 3. Then the stack is empty.
 
Then the two sentences are Ld. i4.4, stloc.1.
It means to press 4 into the stack and then play it out to B.

I feel so confused.
Now the stack is empty, right?
The following is interesting:
Ldloc.0, ldloc.1
I guess it should be the abbreviation of load local.
Press local variable 0 (a) into the stack, and then local variable 1 (B) into the stack.

That is to say, it takes a long time to load the data .. Orz
It seems that the purpose is to ensure that the data in the stack is correct. In addition, there is no way to directly read data without pop-up. I guess.
Why is Microsoft so troublesome? Is there another implicit or unpredictable? Or is it just to tease me?

The purpose of stack pressure is:
Add: add the two numbers at the top of the stack,
Msdn details:
----------------------
Value1 is pushed to the stack.

Value2 is pushed to the stack.

Value2 and value1 pop up from the stack; value1 is added to value2. (Arthur Note: This sentence is ambiguous ..)

The result is pushed to the stack.
-------------------------

The first two steps should beProgramThe worker makes the load. The next two steps are done by the Add command.
Now the value at the top of the stack is the result of adding. We want to assign
Therefore:
Stloc.2
The data at the top of the stack is assigned to variable 2, namely C, and the stack is empty. The void function is finished.

Then ret, apparently return.

This is a void function. What if the returned value is int? For example, return C?
Change the last sentence:
Return A + B;

Reflector tells me that it is like this:
. Maxstack 2
. Locals Init (
[0] int32,
[1] int32 B,
[2] int32 CS $1 $0000)
L_0000: NOP
L_0001: LDC. i4.3
L_0002: stloc.0
L_0003: LDC. i4.4
L_0004: stloc.1
L_0005: ldloc.0
L_0006: ldloc.1
L_0007: add
L_0008: stloc.2
L_0009: Br. s l_000b
L_000b: ldloc.2
L_000c: Ret
This Br. s l_000b is confusing. What does it mean?
BR. S, force redirect. What is the significance of force redirect to the next sentence? I don't understand. What kind of advanced theory does it seem like? For answers...

The end is ldloc.2, that is, press the CS $1 $0000 to the top of the stack to return. That is to say, the return value is thrown to the top of the stack .. This

It seems to be a little different from that when I learned c...

This stack is too imba.

Conclusion: reflector is too powerful.

It's totally because reflector has a very dazzling feature:
When the mouse hover reaches the command, it prompts you what this command means...

Too imba...

Next plan .. Call the function .. See how the parameters are converted ..
How to press it ..

Naughty il ..

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.