What Is A Program?

 < Day Day Up > 



Intuitively you already know the answer to this question. A program is something that runs on a computer. This simple definition works well enough but as a programmer you will need to arm yourself with a better understanding of exactly what makes a program a program. In this section I will discuss programs from two aspects: the computer and the human. You will find this information extremely helpful and it will tide you over until you take a formal course on computer architecture.

Two Views of a Program

A program is a set of programming language instructions and any data the instructions act upon or manipulate. This is a reasonable definition and if you are a human it will do; If you are a processor it just will not fly. That is because humans are great abstract thinkers and computers are not, so it is helpful to view the definition of a program from two points of view.

The Human Perspective

Humans are the masters of abstract thought; it is the hallmark of our intelligence. High-level, object-oriented languages like C++ give us the ability to analyze a problem abstractly and symbolically express its solution in a form that is both understandable by humans and readable by other programs. By other programs I mean the C++ code a programmer writes must be translated from C++ into machine instructions recognizable by a particular processor. This translation is effected by running a compiler that converts the C++ code to object code targeted to a specific machine.

To a C++ programmer a program is a collection of classes that model the behavior of objects in a particular problem domain. These classes model object behavior by defining object attributes (data) and class methods to manipulate the class attributes. On an even higher level, a program can be viewed as an interaction between objects. This view of a program is convenient for humans.

The Computer Perspective

From the computer’s perspective a program is simply machine instructions and data. Usually both the instructions and data reside in the same memory space. This is referred to as a Von Neumann architecture. In order for a program to run it must be loaded into main memory and the address of the first instruction of the program given to the processor. In the early days of computing programs were coded into computers by hand and then executed. Nowadays all the nasty details of loading programs from auxiliary memory into main memory are handled by an operating system, which, by the way, is a program.

Since both instructions and data reside in main memory how does a computer know when it is dealing with an instruction or with data? The answer to this question will be discussed in detail below but here’s a quick answer: It depends on what the computer is expecting. If a computer reads a memory location expecting to find an instruction and it does, everything runs fine. The instruction is decoded and executed. If it reads a memory location expecting to find an instruction but it is not an instruction, then the decode fails and the computer might lock up!

Concept of Observable Behavior

When you write a C++ program you will use a compiler to translate the source code into a machine readable form. The compiler you use represents an implementation instance of an abstract machine as defined by the ANSI C++ standard.

Various aspects of the abstract machine fall into three categories. They are either implementation-defined, unspecified, or undefined. What does this mean to you the programmer? It means that each compiler writer may implement the abstract machine differently but, given a well-formed C++ program, each implementation should produce the same observable behavior. The best definition of observable behavior comes straight from the ANSI C++ standard.

The observable behavior of the abstract machine is its sequence of reads and writes to volatile data and calls to library I/O functions.

In other words, if you write a well-formed C++ program and compile it with compiler A, and again with another compiler B, both compilers A and B should produce an executable program that when run demonstrates the same observable behavior regardless how the writers of each compiler implemented the operation of the abstract machine. Each compiler manufacturer documents how their product implements the abstract machine. I recommend you get to know your development environment inside and out.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net