AMD x86 manual MOV ECX, Maxsize

Models: x86

1 256
Download 256 pages 58.62 Kb
Page 90
Image 90

AMD Athlon™ Processor x86 Code Optimization

Example 1 (Avoid):

22007E/0 — November 1999

int a[MAXSIZE], b[MAXSIZE], c[MAXSIZE], i;

for (i=0; i < MAXSIZE; i++) { c [i] = a[i] + b[i];

}

MOV

ECX, MAXSIZE

;initialize

loop counter

XOR

ESI, ESI

;initialize

offset into array a

XOR

EDI, EDI

;initialize

offset into array b

XOR

EBX, EBX

;initialize

offset into array c

$add_loop:

 

 

MOV

EAX, [ESI + a]

;get element a

MOV

EDX, [EDI + b]

;get element b

ADD

EAX, EDX

;a[i] + b[i]

 

MOV

[EBX + c], EAX

;write result to c

ADD

ESI, 4

;increment offset into a

ADD

EDI, 4

;increment offset into b

ADD

EBX, 4

;increment offset into c

DEC

ECX

;decrement loop count

JNZ

$add_loop

;until loop

count 0

Example 2 (Preferred):

int a[MAXSIZE], b[MAXSIZE], c[MAXSIZE], i;

for (i=0; i < MAXSIZE; i++) { c [i] = a[i] + b[i];

}

MOV ECX, MAXSIZE-1 ;initialize loop counter

$add_loop:

MOV

EAX, [ECX*4 + a]

MOV

EDX, [ECX*4 + b]

ADD

EAX, EDX

MOV

[ECX*4 + c], EAX

DEC

ECX

JNS

$add_loop

;get element a ;get element b ;a[i] + b[i] ;write result to c ;decrement index ;until index negative

Note that the code in example 2 traverses the arrays in a downward direction (i.e., from higher addresses to lower addresses), whereas the original code in example 1 traverses the arrays in an upward direction. Such a change in the direction of the traversal is possible if each loop iteration is completely independent of all other loop iterations, as is the case here.

In code where the direction of the array traversal can’t be switched, it is still possible to minimize pointer arithmetic by appropriately biasing base addresses and using an index

74

Minimize Pointer Arithmetic in Loops

Page 90
Image 90
AMD x86 manual MOV ECX, Maxsize