User's Manual

Always Pair CALL and RETURN 59
22007E/0November 1999 AMD Athlon Processor x86 Code Optimization
Example 6 Increment Ring Buffer Offset:
//C Code
char buf[BUFSIZE];
int a;
if (a < (BUFSIZE-1)) {
a++;
} else {
a = 0;
}
;-------------
;Assembly Code
MOV EAX, [a] ; old offset
CMP EAX, (BUFSIZE-1) ; a < (BUFSIZE-1) ? CF : NC
INC EAX ; a++
SBB EDX, EDX ; a < (BUFSIZE-1) ? 0xffffffff :0
AND EAX, EDX ; a < (BUFSIZE-1) ? a++ : 0
MOV [a], EAX ; store new offset
Example 7 Integer Signum Function:
//C Code
int a, s;
if (!a) {
s = 0;
} else if (a < 0) {
s = -1;
} else {
s = 1;
}
;-------------
;Assembly Code
MOV EAX, [a] ;load a
CDQ ;t = a < 0 ? 0xffffffff : 0
CMP EDX, EAX ;a > 0 ? CF : NC
ADC EDX, 0 ;a > 0 ? t+1 : t
MOV [s], EDX ;signum(x)
Always Pair CALL and RETURN
When the 12 entry return address stack gets out of
synchronization, the latency of returns increase. The return
address stack becomes out of sync when:
calls and returns do not match
the depth of the return stack is exceeded because of too
many levels of nested functions calls