Page 75 - Programming Microcontrollers in C
P. 75

60     Chapter 1  Introduction to C

                          function prototypes perform the stacking operations for the main
                          program. In the main program, a null is pushed onto the stack to
                          identify the end of the data as it is pulled off of the stack a character
                          at a time. Data are read in a character at a time, and as each character
                          is read in, it is pushed onto a stack. When a new line character is
                          detected, the input phase is stopped, and the data written to the stack
                          is pulled off and printed. When the null is detected, the data have
                          all been pulled off the stack, and the program is ended.
                              In the function above, the function exit() is used. This func­
                          tion is similar to the return operation. Whenever a call to the
                          function exit is executed, the argument is evaluated, any files open
                          for write are flushed and closed, and the control of the computer is
                          returned to the operating system. The evaluation of the argument is
                          returned to the operating system. Whenever a return is encoun­
                          tered, the expression following the return call is evaluated and
                          returned to the calling function. If control is in main() when the
                          return is encountered, the evaluation of the expression is returned
                          to the operating system. Also, from main() all files open for write
                          are flushed and closed. The function exit() and return work
                          the same in main(), but exit() exits a program and returns con­
                          trol to the operating system from anywhere in the program.
                              In a separate compilation, the stack functions are compiled. In
                          that function, the macro definition MAX is defined as 100. Macro
                          definitions can be used to define any character string that is needed
                          in a program. They are not limited to defining pseudo functions. Two
                          external variables are defined in this file: an array of MAX integers
                          named buffer and an int called sp. These variables are declared
                          to be static. As such, these variables can be accessed by any function
                          in the file, but they are not available to any function outside of the
                          file. The variable sp is used as an index into the array buffer. When
                          a push is executed, a test to determine if sp is less than MAX is com­
                          pleted. If sp is less than MAX, the data are stored at the sp location
                          in buffer and sp is then incremented. Otherwise, an error message
                          indicates that a stack overflow has occurred and the program is ex­
                          ited.
                              The pull() function is the reverse of the push() operation.
                          First a check is made to see if there are some data on the stack to be
                          pulled off. If there are data, the stack pointer is decremented, and the
   70   71   72   73   74   75   76   77   78   79   80