XOR register, register (assembler)

From time to time we have to analyze assembler code fragments (IA32), and more than often I come across an instruction that looks like this:

xor ax, ax 

or with other registers: xor dx, dx , xor al, al , ...

What exactly does this do? (ax xor ax always gives 0?)

+6
source share
3 answers

This is a common assembler idiom for setting the register to 0.

xor ax, ax corresponds to ax = ax ^ ax , which, as you already noticed, is effectively ax = 0 .

If I remember correctly, the main advantage is that its code size is less than mov ax, 0

+13
source

This is exactly what it does - zero register contents

+2
source

xor% ax,% ax, as indicated in previous comments, corresponds to ax = ax xor ax. This is essentially set ax = 0. In addition, it also affects / modifies some of the EFLAGS, such as OF, CF, SF, PF or ZF. In this case, the PF and ZF flags will be set.

SF - indicates whether the result of the last operation led to a value whose most significant bit is set to 1.

PF - Indicates whether the number of bits is given by an odd or even binary representation of the result of the last operation.

ZF - set if the result of the mathematical / logical operation is zero or reset otherwise.

An example is shown below using fragments of GDB.

Instruction: xor% ax,% ax

To "xor"

 (gdb) info registers eax 0xaa55 43605 ecx 0x0 0 edx 0x80 128 ebx 0x0 0 esp 0x6f20 0x6f20 ebp 0x0 0x0 esi 0x0 0 edi 0x0 0 eip 0x7c02 0x7c02 eflags 0x2 [ ] cs 0x0 0 ss 0x0 0 ds 0x0 0 es 0x0 0 fs 0x0 0 gs 0x0 0 

After "xor"

 (gdb) info registers eax 0x0 0 --------------------> AX = 0 ecx 0x0 0 edx 0x80 128 ebx 0x0 0 esp 0x6f20 0x6f20 ebp 0x0 0x0 esi 0x0 0 edi 0x0 0 eip 0x7c04 0x7c04 eflags 0x46 [ PF ZF ] --------------------> Flags Set cs 0x0 0 ss 0x0 0 ds 0x0 0 es 0x0 0 fs 0x0 0 gs 0x0 0 
+1
source

Source: https://habr.com/ru/post/901943/


All Articles