Computers and Computing

Read time: 27 minutes (6979 words)

Since you should have already taken an introductory course in computer programming, you should know some of the basics of the modern computer is organized. I will skip the history lesson where I go over how we got here, and we will dive right into our primary concern.

How do we create programs using C++?

Compiled Programs

Most of you took COSC1336 before this class, and learned the basics of programming using Python. While Python is a great language for a number of applications, it does have one big failing: it can be slow! In today’s fast paced world, we need more speed! We want answers NOW, not later, so we must move to a different language, and a different way of processing that language t get the speed we want.

The Python Way

Python, (and Java) both read your program source code and converts it into a different form. That form involves something called “byte codes”, which are a simpler form designed to be easy to process. There is a “byte code interpreter” that reads these “byte codes” and performs actions needed to make the program work. The interpreter is just another program (actually written in C++) that does the real work. Because of this two step process, programs written in Python are not as fast as they might be if we worked harder to use the full power of the computer.

Note

In Python, you never see this happening, but you might notice files ending with “.pyc” in your project directory. These are the “byte code” files.

The C++ Way

C++, and many other high-level languages, is a “compiled language”. In these languages, a special program called a “compiler” reads your program source code, checks that it obeys the rules for writing a program in that language, then translates your code into a set of instructions for one particular computer. Those instructions, called “machine instructions” are built into the processor. The compiler works hard to make sure the resulting set of instructions will run as fast as is possible on that particular computer.

This results in very fast programs, but there is one problem. If you want to run the same program on a different computer (with a different processor or operating system), you need to find a compiler for that particular system and process your program again.

Python can be run on all kinds of machines easily, but you still need to install Python on each machine you want to use.

The Actual Programming process

In this class, we will follow a series of steps to build and run your programs. You will use a number of tools and produce a number of “artifacts” that help the tools build a runnable program. Here is the basic process:

Write your Code

You start off by using an editing tool, hopefully one that can help you get this done, and create a “source file”. That file may contain only one part of a complete program. You may write several different files which together fully define everything needed for your program. The end result of this step is one or more program source code files, each with a name that is supposed to help you remember what part of your program is where. Most such files have an “extension indicating what language they are written in. For C++ programs, the common extension is “.cpp”.

Note

You will build files for C++ that end in “.h”, which means these files are “header” files. More on that later.

Compile Your Code

Next, you will fire up a C++ “compiler” which does two things:

  • Checks the “syntax” of your code to make sure you wrote legal C++ code.

  • Translates that C++ code into a form your computer can actually run

    • Our compiler translates your code into “assembly language” for the processor in your computer. A second tool, called an “assembler” translates that code into something called an “Object file”, which is almost ready to run on your system

Note

Some compilers, go straight to object file form, and do not use an assembler to help out.

If you have any errors in your syntax, the object files are not produced, and you get to fix syntax errors! Not fun, but necessary when you cannot type very well!

Loading Your Code

When you have an “executable file” for your program, you can run it as usual. You might “double click” on an icon, or the executable file name to start this last process. You might also type in a command to the operating system asking it to launch your program for you.

No matter how you start the program up, what actually happens is that an operating system component called a “loader program” reds your executable file, and install all the program code and data into memory. The final result is that the processor can finally run your code. That does not actually happen right away in most modern computers. Instead, your program is added to a list of other programs in memory at the same time, and the processor sweeps over all of those programs giving a little bit of its time to each one in turn. The result of this trickery is that you seem to see all of them running at the same time, when actually ony one is running, and only for a short time. The processor makes sure each program is unaware that it is being started and stopped at a furious rate, letting many things seem to happen at the same time!

Magic!

Nope, just fantastically fast processors doing their thing!

Putting it all together

Here is a diagram showing how all this ties together:

../_images/AssemblyProcess.png

(Substitute compiler where is says assembler.)