xorl %eax - Instruction set architecture in IA-32

Hélder Moreira picture Hélder Moreira · May 10, 2014 · Viewed 10.4k times · Source

I am experiencing some difficulties interpreting this exercise;

What does exactly xorl does in this assembly snippet?

C Code:

int i = 0;
if (i>=55)
    i++;
else
    i--;

Assembly

xorl ____ , %ebx
cmpl ____ , %ebx
Jel  .L2
____ %ebx
.L2:
____ %ebx
.L3:

What's happening on the assembly part?

Answer

cnicutar picture cnicutar · May 10, 2014

It's probably:

xorl %ebx, %ebx

This is a common idiom for zeroing a register on x86. This would correspond with i = 0 in the C code.


If you are curious "but why ?" the short answer is that the xor instruction is fewer bytes than mov $0, %ebx. The long answer includes other subtle reasons.

I am leaving out the rest of the exercise since there's nothing idiosyncratic left.