Does .Net add an extra AND value when setting bool to TRUE?

The following code written in C # in VS2010 for .Net 4.0:

bool b = false; 

has the following disassembly:

 XOR EDX,EDX MOV DWORD PTR[EBP-3Ch],EDX 

which makes perfect sense.

However, the following code:

 bool b = true; 

has the following disassembly:

 MOV EAX,1 AND EAX,0FFh MOV DWORD PTR[EBP-3Ch],EAX 

What is the purpose of the AND operation? Why not just MOV EAX,1 ?

 Line | EAX ----------- 1 | 0x01 2 | 0x01 & 0xFF = 0x01 3 | 0xFF 
+7
source share
3 answers

I suspect and 0xff is truncating to an 8-bit bool. I don’t know almost enough about the internal elements, but I suspect that bool b = true becomes something like bool b = int(1) , which then becomes bool b = bool(int(1)) , and this is forcing bool calls and 0xff . I see similar things in x86 epilogs of C ++ functions returning a bool (e.g. ending with test; setal ), and not just returning an arbitrary non-zero value.

This is the code that the signature optimizer fixed ...

+2
source

This code

 bool a = true; bool c = false; 

generates this IL assembly:

 IL_0001: ldc.i4.1 IL_0002: stloc.0 IL_0003: ldc.i4.0 IL_0004: stloc.1 

You can see in the intermediate language, the code is essentially the same. How jitter translates this, or why it won't be effective, parallel code for two is very strange.

(I put this as an answer instead of a comment so that I can format the discussion for reading.)

+1
source

The I-code operation will also result in the setting of some FLAGS states (OF, CF, SF, ZF and PF) in accordance with Intel guidelines ( http://www.intel.com/content/www/us/en/processors/architectures- software-developer-manuals.html ). Without the context of the rest of the instructions in the function, it's pretty hard to guess what the purpose is.

0
source

All Articles