Is there a way to get around this to decrease code size?
The shortest way seems to be the following:
xor %ax, %ax
mov %ax, %ds
If I didn't make a mistake, this is one byte shorter than the variant using mov $0, %ax
.
Do you know why they chose to not allow segment registers as operands for xor
? That would be interesting to know.
I doubt that the designers of the CPU intended that the operation xor %ax, %ax
was used for zeroing a register. Instead, the CPU designers wanted that it is possible to xor
the ax
register with any other register.
And because it would have been more difficult not to allow xor
-ing ax
with itself, the CPU does not only allow operations on two different registers (like xor %bx, %ax
) but also operations that use the same register for both arguments.
For segment registers this is different:
The only purpose of segment registers is to store a memory segment; these registers are not intended to store any other kind of information.
There are only very few situations where an arithmetic (or bit-wise) operation with memory segments makes sense. One example would be an addition in the case of arrays that are longer than 64 KiB; in such cases an add
operation might have been useful.
However, in most cases you don't perform arithmetic operations on values that represent memory segments.
I think for this reason the designers of the CPU decided not to provide any arithmetic operations, so they could design a less expensive CPU (see Margaret Bloom's comments).