Always Pair Call And Return - AMD Athlon Processor x86 Optimization Manual

X86 code optimization
Table of Contents

Advertisement

22007E/0—November 1999

Always Pair CALL and RETURN

Always Pair CALL and RETURN
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]
CMP
EAX, (BUFSIZE-1)
INC
EAX
SBB
EDX, EDX
AND
EAX, EDX
MOV
[a], EAX
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]
CDQ
CMP
EDX, EAX
ADC
EDX, 0
MOV
[s], EDX
Wh e n t h e 1 2 e n t ry re t u r n a d d re s s s t a ck g e t s o u t o f
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
AMD Athlon™ Processor x86 Code Optimization
; old offset
; a < (BUFSIZE-1) ? CF : NC
; a++
; a < (BUFSIZE-1) ? 0xffffffff :0
; a < (BUFSIZE-1) ? a++ : 0
; store new offset
;load a
;t = a < 0 ? 0xffffffff : 0
;a > 0 ? CF : NC
;a > 0 ? t+1 : t
;signum(x)
59

Advertisement

Table of Contents
loading

Table of Contents