loading...

[Minimal] C set or Assembly xor

amir_rezai profile image Amir Mohammad Rezai Updated on ・1 min read

What?

I wanted to do minimal test in C to see if it's optimized to xor and set or set directly into a variable using C.

xor'ing register with itself is faster than setting it to 0 in assembly. for example:

xor eax, eax ;; FAST
mov eax, 0x0 ;; SLOW

Code

Minimal code:

  • XOR:
void xor_test(){
  int a = 0x111; // Anything
  asm( "xor %0, %0" :"=r"(a));
}
  • C Set:
void set_test(){
  int a = 0x111;
  a = 0;
}

Result

This is the disassembly result in x86_64 system compiled with gcc

  • XOR:
0000000000000000 <xor_test>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   c7 45 fc 11 01 00 00    movl   $0x111,-0x4(%rbp)
   b:   31 c0                   xor    %eax,%eax
   d:   89 45 fc                mov    %eax,-0x4(%rbp)
  10:   90                      nop
  11:   5d                      pop    %rbp
  12:   c3                      retq
  • C Set:
0000000000000000 <set_test>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   c7 45 fc 11 01 00 00    movl   $0x111,-0x4(%rbp)
   b:   c7 45 fc 00 00 00 00    movl   $0x0,-0x4(%rbp)
  12:   90                      nop
  13:   5d                      pop    %rbp
  14:   c3                      retq

Conclusion

The size of the opcodes are exactly the and I'm so confused on which one to use cause the overhead is the same but inside Intel reference they point-out XORing register is faster.
Also I wanted to achieve something like this:
xor -0x4(%rbp), -0x4(%rbp)
in this case I could say xoring is 100% faster but now I have no idea, maybe I have to test using time?
Any opinions?

Posted on by:

amir_rezai profile

Amir Mohammad Rezai

@amir_rezai

I'm a game developer, with performance issues in mind. Live in Nerverland, Iran.

Discussion

markdown guide
 

xor -0x4(%rbp), -0x4(%rbp)

is not possible cause they are both memory references.