Chapter 1

Computer Systems and Software

Programmable software is what makes a computer a powerful tool. Each different program essentially “rewires” the computer to allow it to perform a different task. In this course, you will learn basic principles of writing software in the Python programming language.

Python is a popular scripting language available as a free download from http://www.python.org/. Follow the instructions given there to install the latest production version of Python 3 on your system. The examples in this text were written with Python 3.2.

The CPU and RAM

In order to write software, it will be helpful to be able to imagine what happens inside a computer when your program runs. We begin with a rough picture and gradually fill in details along the way.

When a program is ready to run, it is loaded into RAM, usually from long-term storage such as a hard drive. RAM is an acronym for random access memory, which is the working memory of a computer. RAM is volatile, meaning that it requires electricity to keep its contents.

Once a program is loaded into RAM, the CPU, or central processing unit, executes the instructions of the program, one at a time. Each type of CPU has its own instruction set, and you might be surprised at how limited these instruction sets are. Most instructions boil down to a few simple types: load data, perform arithmetic, and store data. What is amazing is that these small steps can be combined in different ways to build programs that are enormously complex, such as games, spreadsheets, and physics simulations.

Computer Languages

CPU instruction sets are also known as machine languages. The key point to remember about machine languages is that in order to be run by a CPU, a program must be written in the machine language of that CPU. Unfortunately, machine languages are not meant to be read or written by humans. They are really just specific sequences of bits in memory. (We will explain bits later if you do not know what they are.)

Because of this, people usually write software in a higher-level language:

Level

Language

Purposes

Higher

Python

Scripts

Java

Applications

C, C++

Applications, Systems

Assembly Languages

Specialized Tasks

Lower

Machine Languages

This table is not meant to be precise, but, for example, most programmers would agree that C and C++ are closer to the machine than Python.

Compilation and Interpretation

Now if CPUs can only run programs written in their own machine language, how do we run programs written in Python, Java, or C++? The answer is that the program must be translated into machine language first.

There are two main types of translation: compilation and interpretation. When a program is compiled, it is completely translated into machine language, producing an executable file. C and C++ programs are usually compiled, and when they are compiled in Microsoft Windows®, for example, the executable files generally end in .exe.

On the other hand, when a program is interpreted, it is translated “on-the-fly.” No separate executable file is created. Instead, the translator program (the interpreter) is running and it translates your program so that the CPU can execute it. Python programs are usually interpreted.

The Python Interpreter

When you start Python, you are in immediate contact with a Python interpreter. If you provide it with legitimate Python, the interpreter will translate your code so that it can be executed by the CPU. The interpreter displays the version of Python that it will interpret and then shows that it is ready for your input with this prompt:

 >>>

The interpreter will translate and execute any legal Python code that is typed at the prompt. For example, if we enter the following statement:

 >>> print("Hello!")

the interpreter will respond accordingly. Try it and see.

Remember that as you learn new Python constructs, you can always try them out in the interpreter without having to write a complete program. Experiment—the interpreter will not mind.

A Python Program

Still, our focus will be on writing complete Python programs. Here is a short example:

Listing 1.1: Area of a Circle

 1 from math import pi
 2 r = 12
 3 area = pi * r ** 2
 4 print("The area of a circle with radius", r, "is", area)

Even if you have never seen Python before, you can probably figure out what this program does.

Start the Python IDLE application and choose “New Window” from the File menu. Type Listing 1.1 into the new window that appears. Save it as circle.py, and then either choose “Run Module” from the Run menu or press F5 to run the program. This program illustrates many important Python concepts, including variables, numeric expressions, assignment, output, and using the standard library. We will examine these components in the next chapter.

Why Computer Science?

Here are a few things to consider as we begin:

  1. Software is everywhere. If you are skeptical, search for “weather forecast toaster.”
  2. Similarly, computation is having a significant impact on the way other disciplines do their work. And for almost every field X, there is a new interdisciplinary field “Computational X.”
  3. Programming develops your ability to solve problems. Because machine languages are so simplistic, you have to tell the computer everything it needs to do in order to solve a problem. Furthermore, running a program provides concrete feedback on whether or not your solution is correct.
  4. Computer science develops your ability to understand systems. Software systems are among the most complicated artifacts ever created by humans, and learning to manage complexity in a program will help you learn to manage it in other areas.
  5. Programming languages are tools for creation: they let you build cool things. There is nothing quite like getting an idea for a program and seeing it come to life. And then showing it to all your friends.

Exercises

  1. 1.1 Experiment with Listing 1.1 by making changes to various parts of the code. Be sure to try some things that break the program (cause errors), just to see how the interpreter reacts.
  2. 1.2 Modify Listing 1.1 to also compute and display the circumference of the circle.
  3. 1.3 The December 1978 issue of the IEEE Computer Society journal Computer contained the following description of a new computer that fit “on the top of any business desk”:

    The PCC 2000 consists of a 3MHz 8085A microprocessor, two 32K memory boards, two FD514 double-density, 8.5-inch floppy disk drives, a 12-inch upper/lower case video display. . .

    1. (a) Compare the PCC 2000’s CPU speed and amount of RAM to current desktops.
    2. (b) Does the PCC 2000 appear to have a hard disk? If not, what does it use for long-term storage?
    3. (c) Research the capacity of one of these floppy disks.
    4. (d) List possible reasons that the PCC 2000 has two floppy disk drives instead of one.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.78.155