AMD x86 manual Avoid Recursive Functions

Models: x86

1 256
Download 256 pages 58.62 Kb
Page 82
Image 82

AMD Athlon™ Processor x86 Code Optimization

22007E/0 — November 1999

Avoid Recursive Functions

Avoid recursive functions due to the danger of overflowing the return address stack. Convert end-recursive functions to iterative code. An end-recursive function is when the function call to itself is at the end of the code.

Example 1 (Avoid):

long fac(long a)

{

if (a==0) { return (1);

} else {

return (a*fac(a–1));

}

return (t);

}

Example 2 (Preferred):

long fac(long a)

{

long t=1; while (a > 0) {

t *= a; a--;

}

return (t);

}

66

Avoid Recursive Functions

Page 82
Image 82
AMD x86 manual Avoid Recursive Functions