which is the faster instruction, and in what situations?
Thanks
-Lolth
Posted on 2001-12-09 07:42:55 by LOLTH
Well, i suggest you to use xchg when you can. This instruction needs two bytes, against the three bytes that mov needs. It can also save you some typing. :)
Posted on 2001-12-09 09:50:12 by CodeLover
Check this out, from agner.hlp:

The XCHG register, instruction is dangerous. By default this instruction has an implicit LOCK prefix which prevents it from using the cache. This instruction is therefore very time consuming, and should always be avoided.
Posted on 2001-12-09 12:38:21 by Qweerdy
I always use it when I can, and i never had problems.
Posted on 2001-12-09 12:41:40 by CodeLover
Of course you never had *problems*. The "problem" is that of speed...
not "weird effects" or whatever ;).

Hm, the LOCK prefix has special meaning when used in multiprocessor
systems, doesn't it? Guess it's RTFM time ;)
Posted on 2001-12-09 12:44:40 by f0dder
On the Althon 'xchg eax,eax (nop)' is the only version which is DirectPath, the rest are VectorPath and block the decoding of other DirectPath instructions. Therefor, if you had a temp register you could always find a faster way to execute an exchange using mov instructions. With reguard to size - it's the smallest way to exchange two values. From an algorithmic standpoint there is usually a way to eliminate the instruction all together! :)

f0dder, Vol.3, Chpt.7.

Edit: I'm not so sure about my statements above - I think xchg might be faster if you had another VectorPath instruction to pair with it, or couldn't spread out the mov instructions? This excludes the reg,mem/mem,reg versions of xchg - they always take longer.
Posted on 2001-12-09 13:17:43 by bitRAKE
XCHG is old stuff, you can use PUSH/POP or MOV if you have the spare registers. It really depends where you are using it, in the middle of a critical algo, you would do whatever is necessary to avoid it, anywhere else it does not matter.

There will probably be variation across different makes of processors and different model of the same make but from memory, its nly degrees of how much slower XCHG is.

Regards,

hutch@movsd.com
Posted on 2001-12-09 15:52:02 by hutch--
HIII

i was wondering why i should use mov or push . and avoid using XCHG? .. isnt xchg faster than 3moves? ..

here is another method:

XOR ECX,EBX
XOR EBX, ECX
XOR ECX, EBX

can you tell me what is faster?



bye

eko
Posted on 2001-12-10 01:28:15 by eko
Eko:
three xor's will be faster than xchg.
But speed will depend highly on enviroment.
For example
xor ebx,eax
xor eax,ebx
xor ebx,eax
will take 3 clocks on both P5 and P6
but 6! instructions:
xor ebx,eax
add esp,2
xor eax,ebx
sub esi,edi
xor ebx,eax
add ecx,edx
will take also 3 clocks on P5 and just 2 - 2,5 clocks on P6
So in worst case you still would profit with those 3 xor's against
xchg and also you have ability to get additional bonus insurting
independent on xored registers instructions between them.
Which you couldn't do with xchg.
Posted on 2001-12-10 08:09:45 by The Svin
Thanks alot!
Posted on 2001-12-10 17:09:09 by LOLTH