1

Introduction to
Programming



You cannot endow even the best machine with initiative.
Walter Lippmann, A Preface to Politics


Although computers are capable of performing complex and difficult operations, they are inherently simple-minded and docile machines. They must be told exactly what to do, and they must be instructed in a precise and limited language that they can comprehend. These instructions are known as software. The machinery that actually executes the instructions is knownas hardware.

At the hardware level, computers understand only simple commands, such as "copy this number," "add these two numbers," and "compare these two numbers." These modest commands consistute the computer's instruction set and programs written using these instructions are said to be written in the computer's machine language.

One of the surprising aspects of computer science is the rich array of useful operations that can be performed by combining these simple instructions. Unfortunately, it is extremely tiresome to write programs in machine language because even the simplest tasks require many instructions. Moreover, in most machine languages, everything-instructions, data, variables-is represented by binary numbers. Binary numbers are composed entirely of zeroes and ones (each data is called a bit, short for "binary digit"). These programs, consisting of a jumble of zeroes and ones, are difficult to write, read, and maintain.

In the 1940s and 1950s, all programs were written in machine language, or its close cousin, assembly language.Assembly language is a major improvement over machine language, although it is only once removed from the computer's instruction set. In assemblylanguage, each instruction is identified by a short name rather than a number, and variables can be identified by names rather than numbers. Programs written in assembly language require a special program, called an assembler, to translate assembly language instructions into machine instructions. Today, programs are written in assembly language only when execution speed is a high priority.

The vast majority of programs written today are written in languages called high-level languages that were first developed in the 1950s and 1960s. High-level languages allow programmers to write programs in a language more natural to them than the computer's restrictive language.

One can view programming languages as lying along a spectrum with machine languages at one end and human languages, such as French and English, at the other end (see Figure 1-1) . High-level programming languages fall somewhere between these extremes, usually closer to deal with complex objects without worrying about details of the particular computer on which the program is running. Of course programming languages differ from human languages since they are designed solely to manipulate information. They are much more limited and precise than human languages.

1.1 High-Level Programming Languages

Every high-level language requires a compiler or interpreter to translate instructions in the high-level programming language into low-level instructions that the computer can execute. The remainder of this section applies only to compilers. We describe interpreters in Chapter 13.

A compiler is similar to an assembler, but much more complex. There is a one-to-one correspondence between assembly language instructions and machine instructions. In contrast, a single instruction in a high-level language can produce many machine instructions.

The farther a programming language is from a machine language, the more difficult it is for the compiler to perform its task. But languages that are far removed from the computer architecture offer two main advantages:

Once you have lerned a high-level language, you need not be preoccupied with how the compiler translates programs into a machine language. As a result, programs you write for one computer can be executed on another computer merely by recompiling them. This feature is known as software portability. In Figure 1-2, for instance, a single program written in a high-level language is translated into three machine language programs by three separate compilers.

Another advantage of high-level languages is readability. Their relative closeness to human languages makes programns not only easier to write, but easier to read as well. The operation of a well-written program in a high-level language can be readily apparent to a reader because the symbols and instructions resemble human symbols and instructions. In contrast, even the best-written assembly language programs must be closely analyzed to construetheir operation. For example, consider the simple C statement

a = b+c-2;

which assigns the value "b plus c minus 2" to a, where a, b, and c are variables.

In assembly language, this could be written:

LOAD b, %r0
LOAD c, %r1
ADD %r0, %r1
SUB &2, %r1
STORE %r1, a
Obviously, the C version is easier to read and understand.

Closely related to readability is maintainability. Because they are more readable, programs written in high-level languages are much easier to modify and debug.

Despite these advantages, there are prices to pay when using high-level languages. The most important price that must be paid is reduced efficiency. When a compiler translates programs into machine language, it may not translate them into the most efficientmachine code. Just as it is possible to use different words to say the same thing, it is also possible to use different machine instructions to write functionally equivalent programs. Some combinations of instructions execute faster than others. By writing directly in the machine language, it is usually possible to select the faster version. By writing in a high-level language, the programmer has little control over how a compiler translates code. The result, especially when an unsophisticated compiler is used, can be inefficient code.

Nevertheless, high-level languages are superior to machine and assembly languages in most instances. For one thing, sophiscated compilers can perform tricks to gain efficiency that most assembly language programmers would never dream of. The main reason for the superiority od high-level languages, however, is that most of the cost of software development lies in maintainance, where readability and portbility are crucial.

The issues raised here - portability, efficiency, and readability - are central concepts that we will revisit throughout this book. Many of the assumed advantages of high-level languages, such as portability and readability, are realized only through careful programming. Likewise, the disadvantages, such as reduced efficiency, can be mitigated once the language is well understood.



←授業選択画面へ
←鈴木研究室へへ