Skip to main content
Engineering LibreTexts

7: Program Control Flow and Functions

  • Page ID
    76124
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    At this point most programming textbooks cover procedural programming (branching and loops) before covering functions. This is because in HLLs, branching and looping concern programming logic (how to implement algorithms), and functions are more about abstraction and code organization. This text, however, assumes that this is not the first class in programming for readers, and programming logic and functional abstractions are familiar to the reader. Therefore, looping, branching, and functions are not covered to present logic or abstraction, but to explain program control in a CPU when a program is executing. A secondary goal of this textbook is to present procedural logic and functions in a standard (or canonical) format to simplify how to understand these structures.

    In addition, it is the opinion of the author that it is easier to explain program control and organization by covering functions before procedural constructs. There are two reasons for this belief. First, the program control for functions is more straight forward, as functional abstraction can be implemented with simple or no logic. It does not require structures inside of structures, such as looping and if blocks containing other looping and if blocks that are required to implement an algorithm. A properly implemented assembly function should have a single point where it is entered and a single point where it returns. The calling function should have the address of the function and the return point. Any other use of a function will only result in confusion and a strong possibility of an incorrect and probably erroneous program.

    The second reason for covering functions first is that the canonical form of functions should always be followed unless there is a very excellent and compelling reason to violate it (for instance, operating systems using non-reentrant functions because of time or memory constraints). The reasons for this conical form are easier to explain than the forms for looping and branching statements.

    This chapter will proceed as follows. The first section will introduce the Program Counter (pc) register. The section will then implement a simple Increment function that is called from a main function. The use of the pc to control the flow of the program will be shown, and the process of calling the Increment function from a main function and then returning at the end to the main function will be illustrated. This is meant to show how program control is accomplished in a program using the pc.

    The second section of this chapter will explain the issues with the simple program in the first section. It will address these issues, and in the process define the program stack, and why and how to push and pop to the stack.

    The final section will explain how to make references to a function available outside of the current file and using this information to create library assembly files that are collections of functions that are used in multiple programs. Makefiles for the programs using these library files are updated to allow the linker to use the functions when creating programs. The concept of Unix library files will be covered for files containing a large number of function objects.


    This page titled 7: Program Control Flow and Functions is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Charles W. Kann III via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?