22007E/0 — November 1999

AMD Athlon™ Processor x86 Code Optimization

variable that starts with a negative value and reaches zero when the loop expires. Note that if the base addresses are held in registers (e.g., when the base addresses are passed as arguments of a function) biasing the base addresses requires additional instructions to perform the biasing at run time and a small amount of additional overhead is incurred. In the examples shown here the base addresses are used in the d i s p l ac e m e n t p o r t i o n o f t h e a dd re ss a n d b ia s in g i s accomplished at compile time by simply modifying the displacement.

Example 3 (Preferred):

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

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

}

 

 

 

 

MOV

ECX,

(-MAXSIZE)

;initialize index

$add_loop:

 

 

 

MOV

EAX,

[ECX*4 + a + MAXSIZE*4] ;get a

element

MOV

EDX,

[ECX*4 + b + MAXSIZE*4] ;get b

element

ADD

EAX,

EDX

;a[i] + b[i]

MOV

[ECX*4 + c + MAXSIZE*4], EAX ;write

result to c

INC

ECX

 

;increment index

JNZ

$add_loop

;until

index==0

Push Memory Data Carefully

Carefully choose the best method for pushing memory data. To reduce register pressure and code dependencies, follow example 2 below.

Example 1 (Avoid):

MOV EAX, [MEM]

PUSH EAX

Example 2 (Preferred):

PUSH [MEM]

Push Memory Data Carefully

75

Page 91
Image 91
AMD x86 manual Push Memory Data Carefully, Example 3 Preferred