Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XOR register,register (assembler) [duplicate]

Tags:

x86

assembly

xor

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

xor ax, ax

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

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

like image 985
Aerus Avatar asked Nov 20 '11 13:11

Aerus


People also ask

What is Assembly assembly Register?

Assembly - Registers. To speed up the processor operations, the processor includes some internal memory storage locations, called registers. The registers store data elements for processing without having to access the memory. A limited number of registers are built into the processor chip.

What are the extra segment registers in Assembly?

Apart from the DS, CS and SS registers, there are other extra segment registers - ES (extra segment), FS and GS, which provide additional segments for storing data. In assembly programming, a program needs to access the memory locations. All memory locations within a segment are relative to the starting address of the segment.

What is the advantage of using XOR Reg?

Another reason to use XOR reg, reg or XORPS reg, reg is to break dependency chains, this allows the CPU to optimize the parallel execution of the assembly commands more efficiently (even it it adds some more instruction throughput preasure). That gives it an advantage over AND reg, 0, but not over MOV reg, 0.

How does the XOR instruction work?

The XOR instruction connects two values using logical exclusive OR remember OR uses inclusive OR To understand XOR better, consider those two binary values: If you OR them, the result is 1100011011 When two bits on top of each other are equal, the resulting bit is 0. Else the resulting bit is 1.


3 Answers

It's a common assembler idiom to set a register to 0.

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

If I recall correctly the main advantage is that its code-size is smaller than mov ax, 0

like image 160
CodesInChaos Avatar answered Sep 29 '22 22:09

CodesInChaos


That is exactly what it does -- zero the contents of a register

like image 41
Anthony Blake Avatar answered Sep 29 '22 20:09

Anthony Blake


xor %ax, %ax, as stated in earlier comments corresponds to ax = ax xor ax. This 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, PF and ZF flags will be set.

SF - Indicates whether the result of the last operation resulted in a value whose most significant bit is set to 1.

PF - Indicates if the number of set bits is odd or even in the binary representation of the result of the last operation.

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

Example is shown below using GDB snippets.

Instruction: xor %ax,%ax

Before "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
like image 30
scanjee Avatar answered Sep 29 '22 20:09

scanjee