Computer Algorithms  «Prev  Next»
Lesson 5Running a program
Objective Describe what happens when a computer program is executed.

Running Computer Programs

Running a computer program refers to the execution of the instructions in the program by the computer. When you run or execute a program, the operating system plays a key role as it provides the program with access to the resources of the computer in a controlled fashion. The following SlideShow describes in more detail the typical sequence of events that occur when a program is run and how the program, the operating system, and the hardware of the computer interact.

Executing Program
  1. You instruct the operating system to run a program
  2. The operating system loads the program from the hard drive or other long-term storage device into RAM.
  3. The operating system starts the program. In other words, the CPU begins executing the instructions contained within the program.
  4. The program completes. The operating system makes available to other programs any resources that were being used by the program

Compilation into Machine Code

When you compile your program, the compiler translates the C++ source code into machine code. The resulting file consists of machine instructions and information on how to load the program into memory prior to execution. Machine code is sometimes called object code, but we do not use that terminology to avoid confusion with C++ objects. Machine code files usually have the extension

Machine Code

For example, the machine code for the hello program might be stored in hello.obj. The machine code file contains only the translation of the code that you wrote. That is not enough to actually run the program. To display a string on a window, quite a bit of low-level activity is necessary. The authors of the iostream package (which defines cout and its functionality) have implemented all necessary actions and placed the required machine code into a library. A library is a collection of code that has been programmed and translated by someone else, ready for you to use in your program. A special program called the linker takes your machine code file and the necessary parts from the iostream library and builds an executable file.

Figure 2.5 From Source Code to Executable Program

(Figure 2.5 gives an overview of these steps.)

Executable File

The executable file is usually called hello.exe or hello, depending on your computer system. It contains all machine code necessary to run the program. You can run the program by typing hello at a command prompt, or by clicking on the file icon, even after you exit the C++ environment. You can e-mail that file to another user who does not have a C++ compiler or who may not know that there is such a thing as C++, and that person can run the program in the same way. Your programming activity centers around these files. You start in the editor, writing the source file. You compile the program and look at the error messages. You go back to the editor and fix the syntax errors. When the compiler succeeds, the linker builds the executable file. You run the executable file. If you find an error, you can run the debugger to execute it one line at a time. Once you find the cause of the error, you go back to the editor and fix it. You compile, link, and run again to see whether the error has gone away. If not, you go back to the editor. This is called the edit-compile-debug loop. You will spend a substantial amount of time in this loop in the months and years to come.
The next lesson will introduce you to the language of the computer, typically known as machine language or machine code.