Instructions du processeur de référence (juste avant de charger le système d'exploitation) - XCHG vs XOR, XOR, XOR

1.225.000 - moyenne pour XCHG (gauche), 1.280.014 - moyenne pour XOR, XOR, XOR (droite)
1.225.000 - moyenne pour XCHG (gauche), 1.280.014 - moyenne pour XOR, XOR, XOR (droite)

, 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 ( ).

Le code suivant répète l'instruction "XCHG EDI, EAX" 7 fois et l'instruction "XOR" - 21 fois et accumule les ticks de processeur dépensés. Le cycle de chaque commande testée est répété 10 000 fois. Après cela, tout cela défile encore et encore (20 fois au total), en conséquence, la moyenne est calculée. Quant à moi, le test est assez "propre", plus ou moins déterministe. Eh bien, quant à savoir si la commande XCHG et les trois commandes XOR sont égales en termes de durée d'exécution, à en juger par ce test, XCHG est exécuté 5% plus rapidement, ce qui ne correspond pas à ma théorie :)





      +    
             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  
      
      






All Articles