Page 76 - Programming Microcontrollers in C
P. 76

Recursion      61

                          content of the buffer at that location is returned to the calling pro­
                          gram. In the event that sp is 0 when the pull operation is executed, a
                          stack underflow message is sent to the screen prior to exiting the
                          program.
                              The advantage to our making the buffer and the stack pointer in
                          the stack functions static can be easily seen. Suppose that these vari­
                          ables could be accessed from anywhere in the program. In that case,
                          it would not be necessary for the programmer to call the functions
                          push or pull to stack and unstack data. If several different pro­
                          grammers were using the same stack for different tasks in one large
                          program, it would be possible for different programmers to access
                          the stack as expected, or from their own tasks. Suppose a program­
                          mer made the mistake of pre-decrementing the stack pointer on
                          stacking and post-incrementing the stack pointer on unstacking. The
                          whole program would suddenly be in chaos. Therefore, masking these
                          variables from the rest of the program can reduce serious potential
                          debugging problems.

            Recursion

                              A recursive routine is one that calls itself. The C language is
                          supposed to produce recursive code. Compilers for large machines
                          usually support recursion, but recursion is often one of the first casu­
                          alties on small microcontrollers. Automatic variables are created when
                          a function is entered, and they are stored on the stack. Therefore,
                          each time a function is called, a new stack frame is created, and
                          variables in place from an earlier execution of the function are unal­
                          tered. Such a function is called re-entrant, and re-entrant functions
                          are also recursive. An example of a simple recursive function is the
                          factorial:

                   n! = n*(n-1)*(n-2)*....*2*1
                              An interesting observation that can be made of factorial is that

                   n! = n*(n-1)!
                          or n factorial equals n times n-1 factorial. Also, the factorial of 0 is
                          defined as 1. With these definitions, it is possible to write the follow­
                          ing recursive function to calculate the factorial of a number:
                   long factorial( int n)
   71   72   73   74   75   76   77   78   79   80   81