4 ms·
> will require the presence of a register into which to place a^b right before writing it back to a. But then you might as well use this register directly for t
by mortenlarsen 14y ago
> will require the presence of a register into which to place a^b right before writing it back to a. But then you might as well use this register directly for the swap. "Saving" a third variable is in every way a false economy.
I think you will find that you are wrong.
x86 asm:
XOR EAX,EBX
XOR EBX,EAX
XOR EAX,EBX
1. I am not saying that this trick is of much use in most cases.
2. But there CPU/MCU's with only registers and no (data) RAM. Also, very early boot on x86 doesn't have RAM initialized.
3. All registers are not equal, some instructions (on x86) default to certain registers, and it might be useful if no free register is available.
4. Again. Doing this without a good reason is stupid.
- emn13 14y agoThe argument could be made that this is due to the "high level" nature of x86 assembly :-). On the actual physical chip such an instruction does involve intermediate register-like storage, and this storage exists by necessity: After all, on the electrical level, the operation is carried out by waiting for the voltage levels on some output "wires" to stabilize while the intermediate network of transistors is presented the input voltages continually. In this case, that would take 1 clock tick - after which the probability that the output pattern represents XOR is almost 1 (there is a neglible chance of failure). The point is: we cannot predict such circuits precisely enough when the output really is directly connected to the input (that would be a resonating feedback loop, after all - and we only know the input voltage to within some tolerance, not to mention quantummechanical effects), so there's always some gating and that means some intermediate storage. Even when the instruction set hides that from you.
- mortenlarsen 14y agoYou are correct about that. But that is still not the same as using a third temporary (visible) register for the swap that may, or may not be available. > But then you might as well use this register directly for the swap. "Saving" a third variable is in every way a false economy.