, XCHG RISC x86 CISC? , , "X" "Y", , "Z".
X=5, Y=7
Z=Y
Y=X
X=Z
X=7, Y=5
, XCHG, , ...
X=5, Y=7
XCHG X, Y
X=7, Y=5
"XCHG", "XOR CHANGE", . XOR CHANGE? XOR.
X=5, Y=7
XOR X, Y
XOR Y, X
XOR X, Y
X=7, Y=5
, , "XCHG" "XOR, XOR, XOR". , - , .. , . , MBR ( ).
The following code repeats the "XCHG EDI, EAX" instruction 7 times, and the "XOR" instruction - 21 times and accumulates the spent processor ticks. The cycle for each tested command is repeated 10,000 times. After that, all this is scrolled over and over again (20 times in total), as a result, the average is calculated. As for me, the test is pretty "clean", more or less deterministic. Well, as for whether the XCHG command and the three XOR commands are equal in terms of execution duration, judging by this test, XCHG is executed 5% faster, which does not fit into my theory :)
+
mov ax, 3
int 10h
cli ;
mov al, 0FFh
out 021h, al
out 0A1h, al
mov cx, 20 ; 20
again: push cx
xor ebp, ebp
mov si, 10000 ; 10000 XCHG
@@: xor eax, eax
xor edi, edi
cpuid ;
rdtsc
rept 7 { xchg edi, eax } ; 7 XCHG
cpuid
rdtsc
sub eax, edi ;
add ebp, eax ;
dec si
jnz @B
mov [_xchg], ebp
xor ebp, ebp
mov si, 10000 ; 10000 XOR
@@: xor eax, eax
xor edi, edi
cpuid
rdtsc
rept 7 { xor edi, eax ; 7 XOR
xor eax, edi
xor edi, eax }
cpuid
rdtsc
sub eax, edi
add ebp, eax
dec si
jnz @B
next: mov [_xor], ebp
mov eax, [_xchg]
add [totalxchg], eax
mov di, [screen]
call print
add word [screen], 32
mov eax, [_xor]
add [totalxor], eax
mov di, [screen]
call print
add word [screen], 128
pop cx
dec cx
jnz again
dec byte [color+1]
mov eax, [totalxchg]
mov ebx, 20
xor edx, edx
idiv ebx
mov di,[screen]
call print
mov eax, [totalxor]
mov ebx, 20
xor edx, edx
idiv ebx
add word [screen], 32
mov di,[screen]
call print
@@: jmp @B ; .
print: mov ebx, 10 ;
xor cx, cx
more: mov si, bufferdec+12
xor edx, edx
sub si, cx
idiv ebx
add dl, '0'
mov [si], dl
inc cx
test cl, 1
je @F
test cl, 2
je @F
or eax, eax
je @F
mov [si-1], byte '.'
inc cx
@@: or eax, eax
jne more
color: mov ah, 7
push 0b800h
pop es
mov si, bufferdec+12
add di, cx
add di, cx
std
@@: lodsb
stosw
loop @b
push cs
pop es
ret
screen: dw 0
_xchg: dd 0
totalxchg: dd 0
_xor: dd 0
totalxor: dd 0
bufferdec: db 12 dup 0
rb 510 - ($ - $$)
db 55h,0AAh