, 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 ( ).
Kode berikut mengulangi instruksi "XCHG EDI, EAX" 7 kali, dan instruksi "XOR" - 21 kali dan mengakumulasi kutu prosesor yang dihabiskan. Siklus untuk setiap perintah yang diuji diulang 10.000 kali. Setelah itu, semua ini digulir berulang-ulang (total 20 kali), sebagai hasilnya, rata-rata dihitung. Bagi saya, tes ini cukup "bersih", kurang lebih deterministik. Nah, apakah perintah XCHG dan ketiga perintah XOR sama dalam hal durasi eksekusi, dilihat dari tes ini, XCHG dieksekusi 5% lebih cepat, yang tidak sesuai dengan teori saya :)
+
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