Linked Questions
215 questions linked to/from What is the best way to set a register to zero in x86 assembly: xor, mov or and?
162
votes
6
answers
118k
views
What is the purpose of XORing a register with itself? [duplicate]
xor eax, eax will always set eax to zero, right? So, why does MSVC++ sometimes put it in my executable's code? Is it more efficient that mov eax, 0?
012B1002 in al,dx
012B1003 push ...
67
votes
6
answers
26k
views
Does using xor reg, reg give advantage over mov reg, 0? [duplicate]
There're two well-known ways to set an integer register to zero value on x86.
Either
mov reg, 0
or
xor reg, reg
There's an opinion that the second variant is better since the value 0 is not stored ...
3
votes
2
answers
9k
views
xor ax, ax when loading segment register [duplicate]
I'm trying to write a boot loader, and all the code I am writing is being run in real mode. In all the examples I find there is either an xor ax, ax or xor eax, eax, and I don't understand what this ...
4
votes
1
answer
214
views
Instruction which results in 0 but isn't dependency-breaking [duplicate]
I've developed a little benchmark. The result of the loop inside this benchmark should be transformed to zero and the next round of my calculation should depend on the zero-"result" of the ...
1
vote
0
answers
452
views
best methods to clear rax [duplicate]
The popular method to clear rax is xor rax,rax (0x4831C0) but that operation affects the flag bits.
How to clear rax without affecting the flags?
For example:
mov rax,0 (0x48C7C0xxxxxxxx takes 7 ...
0
votes
0
answers
256
views
Why GCC uses xor to clear a register? [duplicate]
Compiling the following C program with GCC 6.4.1 using -O0 and -O2 gives the following results for the main() function.
int main(int argc, char *argv[]) {
if (argc == 2) {
printf("Checking ...
0
votes
0
answers
256
views
Does setcc zero-extend the register? [duplicate]
A setcc instruction writes to the low byte of a register. If I write, e.g., "setnl cl" will ecx/rcx be zero-extended?
EDIT: after the comments made below, I see that setcc does not zero-...
1
vote
0
answers
196
views
Why is zeroing a register via xorl faster than doing so via movl? [duplicate]
In Programming from the Ground Up one reads that
the XOR operation is faster than the loading operation, so many programmers use it to load a register with a zero. For example, the code
movl $0, %eax
...
959
votes
11
answers
189k
views
Why does C++ code for testing the Collatz conjecture run faster than hand-written assembly?
I wrote these two solutions for Project Euler Q14, in assembly and in C++. They implement identical brute force approach for testing the Collatz conjecture. The assembly solution was assembled with:
...
146
votes
23
answers
117k
views
Position of least significant bit that is set
I am looking for an efficient way to determine the position of the least significant bit that is set in an integer, e.g. for 0x0FF0 it would be 4.
A trivial implementation is this:
unsigned ...
187
votes
4
answers
56k
views
Why do x86-64 instructions on 32-bit registers zero the upper part of the full 64-bit register?
In the x86-64 Tour of Intel Manuals, I read
Perhaps the most surprising fact is that an instruction such as MOV EAX, EBX automatically zeroes upper 32 bits of RAX register.
The Intel documentation (...
58
votes
11
answers
143k
views
What is the meaning of XOR in x86 assembly?
I'm getting into assembly and I keep running into xor, for example:
xor ax, ax
Does it just clear the register's value?
54
votes
12
answers
12k
views
How does an assembly instruction turn into voltage changes on the CPU?
I've been working in C and CPython for the past 3 - 5 years. Consider that my base of knowledge here.
If I were to use an assembly instruction such as MOV AL, 61h to a processor that supported it, ...
73
votes
4
answers
6k
views
Why is memcmp(a, b, 4) only sometimes optimized to a uint32 comparison?
Given this code:
#include <string.h>
int equal4(const char* a, const char* b)
{
return memcmp(a, b, 4) == 0;
}
int less4(const char* a, const char* b)
{
return memcmp(a, b, 4) < 0;
...
48
votes
10
answers
89k
views
How many ways to set a register to zero?
I'm curious how many ways are there to set a register to zero in x86 assembly. Using one instruction. Someone told me that he managed to find at least 10 ways to do it.
The ones I can think of are:
...