Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does the == operator do?

So I took a look at ILDASM, inspecting a .exe which looks like this:

int a = 2;
Int32 b = 1;
if(b == 1)
{

}

Now, the CIL code looks like that:

IL_0005:  ldloc.1
IL_0006:  ldc.i4.1
IL_0007:  ceq
IL_0009:  ldc.i4.0
IL_000a:  ceq
IL_000c:  stloc.2

I understand that first b is loaded (which is stored at [1]), then a constant with the value of 1 and then they are compared. What I do not understand is why another constant with the value 0 is loaded and compared, before the result of that comparison is stored.
Since the first compare should already produce a truth value, checking if this value is 0 inverts the result, right?
My question is now: why is it inverted? I assume it has something to do with the == operator that I used and my theory is that it returns the difference. If this difference is 0, the values are the same, so true should be the result. But 0 stands for false, so it needs to be inverted.
I just can't seem to find anything about this topic, just something about operators like ==~ or the likes. Hope you can enlighten me :)

Best regards

Wilsu

PS: This is the full code:

.method private hidebysig instance void  Form1_Load(object sender,
                                                class [mscorlib]
System.EventArgs e) cil managed
{
// Code size       19 (0x13)

.maxstack  2
.locals init ([0] int32 a,
       [1] int32 b,
       [2] bool CS$4$0000)
IL_0000:  nop
IL_0001:  ldc.i4.2
IL_0002:  stloc.0
IL_0003:  ldc.i4.1
IL_0004:  stloc.1
IL_0005:  ldloc.1
IL_0006:  ldc.i4.1
IL_0007:  ceq
IL_0009:  ldc.i4.0
IL_000a:  ceq
IL_000c:  stloc.2
IL_000d:  ldloc.2
IL_000e:  brtrue.s   IL_0012
IL_0010:  nop
IL_0011:  nop
IL_0012:  ret
} // end of method Form1::Form1_Load
like image 250
Wilsu Avatar asked Sep 27 '22 05:09

Wilsu


2 Answers

ceq takes two values from the stack and results in 1 if they are considered equal, and 0 if they aren't. However, whether == in C# results in ceq depends on a lot of things:

  • the data types
    • are they primatives?
    • do they have custom == operators?
    • are they references?
  • the context
    • can it be optimized to something else? (I get a bne.un.s in a similar example; there is also beq*, br*, switch, etc)
    • can it be removed completely?
like image 167
Marc Gravell Avatar answered Sep 30 '22 06:09

Marc Gravell


It's doing a jump to the end of the function, as far as I can gather.

void Main()
{
    int a = 2;
    Int32 b = 1;
    if(b == 1)
    {
        Console.WriteLine("A");
    }
}

Gives me:

IL_0000:  nop         
IL_0001:  ldc.i4.2    
IL_0002:  stloc.0     // a
IL_0003:  ldc.i4.1    
IL_0004:  stloc.1     // b
IL_0005:  ldloc.1     // b
IL_0006:  ldc.i4.1    
IL_0007:  ceq         
IL_0009:  ldc.i4.0    
IL_000A:  ceq         
IL_000C:  stloc.2     // CS$4$0000
IL_000D:  ldloc.2     // CS$4$0000
IL_000E:  brtrue.s    IL_001D
IL_0010:  nop         
IL_0011:  ldstr       "A"
IL_0016:  call        System.Console.WriteLine
IL_001B:  nop         
IL_001C:  nop         
IL_001D:  ret         

Starting from IL_0005, we have:

Load b.
Load 1.
ceq (If equal, push 1, if false push 0) - Result here will be 1
Load 0
ceq - Result here will be 0
brtrue.s IL_001D - If value is non-zero, jump to IL_001D (end of function)

So it's essentially compiled to this:

int a = 2;
Int32 b = 1;
if(!(b == 1))
    goto end;
Console.WriteLine("A");
:end
return;
like image 31
Rob Avatar answered Sep 30 '22 06:09

Rob